How to run a Python unit test with the Atom editor?

Installation

  1. Install the Atom editor
  2. Install the Script package like this:

    a) Start Atom

    b) Press Ctrl+Shift+P, type "install packages and themes" and press Enter to open the package view

    c) Search for "script" and install the package

Unit test example test.py

  1. Write a unit test and save it as test.py.

    import unittest
    
    class MyTest(unittest.TestCase):
    
      def test_pass(self):
          pass
    
      def test_fail(self):
          call_method_that_does_not_exist()
    
    if __name__ == '__main__':
    unittest.main()
    

Run unit test

  1. Now, press Ctrl+I to run the Python script (see documentation)

Console output

Because the unit test test_fail will fail, this will be the console output:

E.
======================================================================
ERROR: test_fail (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Lernkurve/Desktop/PythonDemos/a.py", line 9, in test_fail
    call_method_that_does_not_exist()
NameError: global name 'call_method_that_does_not_exist' is not defined

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (errors=1)
[Finished in 0.047s]

You could use the Atom Python Test plug-in. It supports:

  • Run the test under cursor
  • Run all tests of a module
  • Run doc tests

It also supports adding additional arguments to test execution and allows to run unitttest.TestCase too.