Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

nose

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 e.g. 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).

like image 318
Alex Avatar asked Dec 06 '17 09:12

Alex


2 Answers

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'.

like image 159
Alex Avatar answered Sep 20 '22 19:09

Alex


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 ^^

like image 23
Joabe Lucena Avatar answered Sep 20 '22 19:09

Joabe Lucena