4

This is my first few lines of code but I've coded for 20 years so I quickly wanted to get unit testing running.

I am using

  • Windows 10
  • VS Code 1.30.2 from 7th January, 2019.
  • Python 3.7.2
  • Python Extension ms-python.python 2018.12.1

Here's the contents of the folder I'm in.

    Directory: C:\DATA\Git\Py\my_first_code


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       19/01/2019     21:42                __pycache__
-a----       19/01/2019     21:35            289 messing.py
-a----       19/01/2019     21:42            204 test_messing.py
-a----       19/01/2019     22:07              0 __init__.py

I am not in a "venv", as far as I know.

This is the contents of test_messing.py.

import unittest

class Test_Math(unittest.TestCase):
    def math_multiply__when__2_times_2__then__equals_4(self):
        self.assertEqual(2 * 2, 4)

if __name__ == '__main__':
    unittest.main()

The __init__.py is empty, I added it to see if it helps, and messing.py contains like a 8 lines of some code from a book.

When I try to discover the tests in VS Code I get.

No tests discovered, please check the configuration settings for the tests. Source: Python (Extension)

More interestingly, running test discovery via the Python command line looks like this.

PS C:\DATA\Git\Py\my_first_code> python -m unittest discover -v -s . -p test_*.py

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Soviut
  • 88,194
  • 49
  • 192
  • 260
Luke Puplett
  • 42,091
  • 47
  • 181
  • 266

1 Answers1

6

As it says in the documentation for the unittest module, Your test method names need to begin with test.

tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.

For example

class TestMath(unittest.TestCase):
    def test_multiply(self):
        self.assertEqual(2 * 2, 4)

    def test_multiply_negative(self):
        self.assertEqual(-2 * -2, 4)
        self.assertEqual(2 * -2, -4)

    # etc...

Note that none of these actually test your messing.py functionality. In order to do that you need to import messing, call functions on it, and assert the values those functions return are expected.

Finally, a few conventions you should follow:

  • Use short, simple test names
  • Avoid using double underscores since those often refer to "magic" variables in Python
  • Don't refer to what you're testing in every test since the suite itself already refers to it by name
  • Your class names should not contain underscores
Soviut
  • 88,194
  • 49
  • 192
  • 260
  • 1
    Test _methods_ need to start with test..! Even after I read your answer - which has great advice gratefully received, thanks - I edited my code and still got it wrong. Facepalm. I had it the exact other way around, that the class name needed `Test*` and the methods didn't matter. – Luke Puplett Jan 19 '19 at 23:08
  • 1
    OMG, I was pulling my hair out... No tests discovered, again and again. All I needed to do was rename the method to test_*(): ..... :D – Sean McCarthy Nov 09 '19 at 00:12