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?
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.
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…'.
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.)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With