DEV Community

CJ
CJ

Posted on • Originally published at chuanjin.me

1 1

nosetests example

A good practice to use nose for testing.

from nose import with_setup
def setup_module(module):
print ("") # this is to get a newline after the dots
print ("Start testing module: " + str(module))
def teardown_module(module):
print ("") # this is to get a newline after the dots
print ("Done testing module: " + str(module))
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_numbers_3_4():
print 'test_numbers_3_4 <============================ actual test code'
assert 3*4 == 12
@with_setup(my_setup_function, my_teardown_function)
def test_strings_a_3():
print 'test_strings_a_3 <============================ actual test code'
assert 'a'*3 == 'aaa'
class TestUM:
def setup(self):
print ("TestUM:setup() before each test method")
def teardown(self):
print ("TestUM:teardown() after each test method")
@classmethod
def setup_class(cls):
print ("Start testing class: " + str(cls))
@classmethod
def teardown_class(cls):
print ("Done testing class: " + str(cls))
def test_numbers_5_6(self):
print 'test_numbers_5_6() <============================ actual test code'
assert 5*6 == 30
def test_strings_b_2(self):
print 'test_strings_b_2() <============================ actual test code'
assert 'b'*2 == 'bb'
view raw nosetest.py hosted with ❤ by GitHub

Execution results shows below:

References:
http://pythontesting.net/framework/nose/nose-introduction/

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more