简体   繁体   中英

How to define a setup method only called once during testing with nosetest?

I am running a couple of nosetests with test cases in different modules (files) each containing different tests.

I want to define a function/method that is only called once during the execution with nosetest .

I looked at the documentation (and eg here ) and see there are methods like setup_module etc. - but where and how to use them? Put them into my __init__.py ? Something else?

I tried to use the following:

class TestSuite(basicsuite.BasicSuite):
    def setup_module(self):
        print("MODULE")

    ...

but this printout is never done when I run the test with nosetest . I also do not derive from unittest.TestCase (which will result in errors).

When looking at a package level, you can define a function named setup in the __init__.py of that package. Calling the tests in this package, the setup function in the __init__.py is called once.

Example setup

- package
    - __init__.py
    - test1.py
    - test2.py

See documentation section 'Test packages'.

Try this one

from nose import with_setup

def my_setup_function():
    print ("my_setup_function")

def my_teardown_function():
    print ("my_teardown_function")

@with_setup(my_setup_function, my_teardown_function)
def test_my_cool_test():
    assert my_function() == 'expected_result'

Holp it helps ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM