Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test Theories in Python?

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.

like image 634
Adam Parkin Avatar asked May 27 '15 22:05

Adam Parkin


People also ask

What are unit tests in Python?

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.

What is the goal of unit testing Python?

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.

What is theory in unit testing?

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.


1 Answers

It looks like pytest can do something like this: https://docs.pytest.org/en/latest/parametrize.html

I haven't tried it out myself.

like image 97
elbows Avatar answered Sep 23 '22 22:09

elbows