pythonunit-testingpython-unittestpython-module

Python unittest - Ran 0 tests in 0.000s


So I want to do this code Kata for practice. I want to implement the kata with tdd in separate files:

The algorithm:

# stringcalculator.py  
def Add(string):
   return 1

and the tests:

# stringcalculator.spec.py 
from stringcalculator import Add
import unittest

class TestStringCalculator(unittest.TestCase):
    def add_returns_zero_for_emptyString(self):
        self.assertEqual(Add(' '), 0)

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

When running the testfile, I get:

Ran 0 tests in 0.000s

OK

It should return one failed test however. What do I miss here?


Solution

  • As stated in the python unittest doc:

    The simplest TestCase subclass will simply implement a test method (i.e. a method whose name starts with test)

    So you will need to change your method name to something like this:

    def test_add_returns_zero_for_emptyString(self):
        self.assertEqual(Add(' '), 0)