The configuration and disassembly functions are performed once for all tests on the net.

How to perform setup and disassembly functions once for all tests of netset?

def common_setup(): #time consuming code pass def common_teardown(): #tidy up pass def test_1(): pass def test_2(): pass #desired behavior common_setup() test_1() test_2() common_teardown() 

Note that there is a similar question with an answer that does not work with python 2.7.9-1, python-unittest2 0.5.1-1 and python- nose 1.3.6-1 after replacing the points with pass and adding the import unittest . Unfortunately, my reputation is too low to comment on this.

+1
source share
1 answer

You may have a module level setting function. According to the nose documentation :

Tested modules offer installation and disassembly at the module level; define a setup method, setup_module , setUp or setUpModule to configure, teardown_module , or tearDownModule to delete.

So, more specifically, for your case:

 def setup_module(): print "common_setup" def teardown_module(): print "common_teardown" def test_1(): print "test_1" def test_2(): print "test_2" 

Running the test gives you:

 $ nosetests common_setup_test.py -s -v common_setup common_setup_test.test_1 ... test_1 ok common_setup_test.test_2 ... test_2 ok common_teardown ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK 

It doesn't matter which name you choose, so both setup and setup_module will work the same way, but setup_module has great clarity.

+2
source

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


All Articles