Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 317
MattSom Avatar asked May 13 '17 20:05

MattSom


People also ask

Is Pytest better than Unittest?

Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.

How do I run a Unittest test?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.

Is PyUnit the same as Unittest?

PyUnit is an easy way to create unit testing programs and UnitTests with Python. (Note that docs.python.org uses the name "unittest", which is also the module name.)


1 Answers

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) 
like image 128
Taku Avatar answered Sep 21 '22 10:09

Taku