Python unittests with multiple settings?

I am working on a module using sockets with hundreds of test cases. It's good. Except now I need to check all cases with and without socket.setdefaulttimeout (60) ... Please do not tell me to cut and paste all tests and set / delete the default timeout in setting / detaching.

Honestly, I understand that every test case superimposed on it is good practice, but I also don't like to repeat it myself. This is really just testing in a different context, not different tests.

I see that unittest supports level / settings for the module level, but it is not obvious to me how to convert my one test module into testing myself twice with two different settings.

any help would be greatly appreciated.

+7
source share
5 answers

You could do something like this:

class TestCommon(unittest.TestCase): def method_one(self): # code for your first test pass def method_two(self): # code for your second test pass class TestWithSetupA(TestCommon): def SetUp(self): # setup for context A do_setup_a_stuff() def test_method_one(self): self.method_one() def test_method_two(self): self.method_two() class TestWithSetupB(TestCommon): def SetUp(self): # setup for context B do_setup_b_stuff() def test_method_one(self): self.method_one() def test_method_two(self): self.method_two() 
+4
source

I would do it like this:

  • Make all your tests derive from your own TestCase class, call SynapticTestCase.

  • In SynapticTestCase.setUp (), check the environment variable to determine whether to set the socket timeout or not.

  • Run the entire test suite twice, once, when the environment variable is set in one direction, and then set it again in a different way.

  • Write a small shell script to invoke the test suite in both directions.

+4
source

Other answers to this question are valid to the extent that they allow you to run tests in several environments, but, playing with the options, I think I like a more independent approach. I use kits and results to organize and display test results. To run one test with two environments, not two tests, I took this approach - create a subclass of TestSuite.

 class FixtureSuite(unittest.TestSuite): def run(self, result, debug=False): socket.setdefaulttimeout(30) super().run(result, debug) socket.setdefaulttimeout(None) ... suite1 = unittest.TestSuite(testCases) suite2 = FixtureSuite(testCases) fullSuite = unittest.TestSuite([suite1,suite2]) unittest.TextTestRunner(verbosity=2).run(fullSuite) 
+4
source

If your code does not call socket.setdefaulttimeout , you can run the tests as follows:

 import socket socket.setdeaulttimeout(60) old_setdefaulttimeout, socket.setdefaulttimeout = socket.setdefaulttimeout, None unittest.main() socket.setdefaulttimeout = old_setdefaulttimeout 

This is a hack, but it can work

+1
source

You can also inherit and restart the original set, but overwrite the entire set or part of it:

 class TestOriginal(TestCommon): def SetUp(self): # common setUp here self.current_setUp() def current_setUp(self): # your first setUp pass def test_one(self): # your test pass def test_two(self): # another test pass class TestWithNewSetup(TestOriginal): def current_setUp(self): # overwrite your first current_setUp 
0
source

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


All Articles