How can I define one setup function for all of the net test tests?

I use the Google engine to work with python and want to run some tests using nosetest. I want each test to perform the same setup function. I already have many tests, so I don’t want to go through them all and copy and paste the same function. can I define one tuning function somewhere, and will each test run it first?

thanks.

+3
source share
1 answer

You can write your customization function and apply it using the with_setup decorator:

 from nose.tools import with_setup def my_setup(): ... @with_setup(my_setup) def test_one(): ... @with_setup(my_setup) def test_two(): ... 

If you want to use the same setting for several test cases, you can use a similar method. First you create a setup function, then apply it to all test cameras using the decorator:

 def my_setup(self): #do the setup for the test-case def apply_setup(setup_func): def wrap(cls): cls.setup = setup_func return cls return wrap @apply_setup(my_setup) class MyTestCaseOne(unittest.TestCase): def test_one(self): ... def test_two(self): ... @apply_setup(my_setup) class MyTestCaseTwo(unittest.TestCase): def test_one(self): ... 

Or else, you can simply assign your setting:

 class MyTestCaseOne(unittest.TestCase): setup = my_setup 
+3
source

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


All Articles