In a previous life I did a fair bit of Java development, and found JUnit Theories to be quite useful. Is there any similar mechanism for Python?
Currently I'm doing something like:
def some_test(self):
cases = [('some sample input', 'some expected value'),
('some other input', 'some other expected value')]
for value, expected in cases:
result = method_under_test(value)
self.assertEqual(expected, result)
But this is rather clunky, if the first "case" fails, all others fail to be run.
A unit test is a test that checks a single component of code, usually modularized as a function, and ensures that it performs as expected. Unit tests are an important part of regression testing to ensure that the code still functions as expected after making changes to the code and helps ensure code stability.
Goal of python unit testing is to detect as many bugs and inconsistencies in the infancy of the application development as possible. This is achieved by designing and scripting accurate and quality unit tests that can also serve as detailed documentation for the development process.
Facts and theories While facts are used to test invariant conditions, theories are tests that are true for a particular set of data passed as argument to the method. You would typically use the [Fact] attribute to write unit tests that have no method arguments.
It looks like pytest can do something like this: https://docs.pytest.org/en/latest/parametrize.html
I haven't tried it out myself.
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