Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying out the Atom editor and was wondering how I can run Python unit tests with a keyboard shortcut.

like image 923
Lernkurve Avatar asked Jan 17 '15 00:01

Lernkurve


People also ask

How do I run python code in Atom editor?

Now run your Python script. If you are running Atom on Windows, type Shift-Ctrl B on the keyboard (hold down Shift and Ctrl and then type B). If you are on the Mac, type command-i (hold down the command key and then the letter i). In either case, you should see hello Atom!

Can Atom editor used for python?

Code Execution in Atom Python Generally, we use the command prompt or terminal to execute Python programs. However, Atom provides a plugin known as platformio-ide-terminal in order to execute the python code. We can set up this plugin by navigating to the File in the Menu bar. Go to Settings.


1 Answers

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]
like image 72
Lernkurve Avatar answered Sep 25 '22 05:09

Lernkurve