How can I determine the installation and demolish all the tests inside the module

I use nosetest as a testing platform, and all my tests are functions.
The tested functions are not part of the class.

I will not decorate each function with the help of the installation, instead I want to define a tuning and breaking function that will be recorded once and will work before and after each test in the module,

Does anyone know of an elegant way of doing this?

+4
source share
2 answers

This is the default behavior unittest:

test.py:
import unittest

class TestFixture(unittest.TestCase):
    def setUp(self):
        print "setting up"

    def tearDown(self):
        print "tearing down"

    def test_sample1(self):
        print "test1"

    def test_sample2(self):
        print "test2"

Here's what it does:

    $ python -m unittest test
    setting up
    test1
    tearing down
    .setting up
    test2
    tearing down
    .
    ----------------------------------------------------------------------
    Ran 2 tests in 0.000s

    OK
+2
source

, . -, , , decorators.py:

def setup_func():
    "set up test fixtures"

def teardown_func():
    "tear down test fixtures"

tests.py :

from decorators import setup_func, teardown_func
from inspect import getmodule
from nose.tools import with_setup
from types import FunctionType

, . , :

for k, v in globals().items():
    if isinstance(v, FunctionType) and getmodule(v).__name__ == __name__:
        v = with_setup(setup_func, teardown_func)(v)

, tests.py ( ) .

, , nose , . , , . . , , - , .

: , , , . , , @with_setup .. TestCase. , , , .

+1

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


All Articles