Parameterization of unit tests in python

I am working on a suite of python unit tests that are currently built using pythons built into the test environment. I would like to write parameterized tests that will be run several times based on the data set that I give them.

i.e. if my dataset is [1,2,3,4], my test function will work four times using the input in my dataset.

def test(data): if data > 0: #Pass the test 

In my opinion, this is currently not possible in the inline structure unless I put a loop in my test function. I do not want to do this because I need the test to continue to run even if one input fails.

I saw that this can be done using the nose or pyTest. What is the best structure to use? Is there any other structure that I could use, would it be better than any of these?

Thanks in advance!

+6
source share
2 answers

Note that this is just one of the most common uses for the recent addition of funcargs to py.test .

In your case, you will receive:

 def pytest_generate_tests(metafunc): if 'data' in metafunc.funcargnames: metafunc.parametrize('data', [1,2,3,4]) def test_data(data): assert data > 0 

[EDIT] Perhaps I should add that you can also do this simply as

 @pytest.mark.parametrize('data', [1,2,3,4]) def test_data(data): assert data > 0 

So, I would say that py.test is a great environment for parameterized unit testing ...

+6
source

You can dynamically create tests based on your data as follows:

 import unittest data_set = [1,2,3,4] class TestFunctions(unittest.TestCase): pass # all your non-dynamic tests here as normal for i in data_set: test_name = "test_number_%s" % i # a valid unittest test name starting with "test_" def dynamic_test(self, i=i): self.assertTrue(i % 2) setattr(TestFunctions, test_name, dynamic_test) if __name__ == '__main__': unittest.main() 

Python unittest question: programmatically generate multiple tests? discusses this more, including another approach that provides the same thing, dynamically creating multiple instances of the test case in the test case.

+1
source

Source: https://habr.com/ru/post/909318/


All Articles