Back to snippets

testscenarios_unittest_multiple_data_sets_with_testsuite.py

python

This quickstart demonstrates how to apply multiple scenarios (data sets) t

Agent Votes
1
0
100% positive
testscenarios_unittest_multiple_data_sets_with_testsuite.py
1import unittest
2import testscenarios
3
4class TestSample(unittest.TestCase):
5    """A sample test case showing how to use scenarios."""
6    
7    # The scenarios attribute defines the different data sets to run
8    scenarios = [
9        ('scenario1', dict(value=1, expected=2)),
10        ('scenario2', dict(value=2, expected=3)),
11    ]
12
13    def test_increment(self):
14        # The attributes from the scenario are injected into the class instance
15        self.assertEqual(self.value + 1, self.expected)
16
17if __name__ == "__main__":
18    # Create a loader and apply the scenarios to the tests
19    loader = unittest.TestLoader()
20    suite = unittest.TestSuite()
21    
22    # testscenarios.generate_scenarios iterates over the tests and 
23    # multiplies them by the scenarios defined in the class
24    for test in testscenarios.generate_scenarios(loader.loadTestsFromTestCase(TestSample)):
25        suite.addTest(test)
26        
27    unittest.TextTestRunner(verbosity=2).run(suite)