nose的使用
简单的例子:
test1,简单的能够运行
temperature1.py
def to_celsius (t): return round ( (t-32.0)*5.0/9.0 ) def above_freezing (t): return t>0
test_temperature.py
import nose from python2.temperature1 import * def test_above_freezing (): '''''Test above_freezing ''' assert above_freezing(89.4), 'A temperature above freezing' assert not above_freezing(-42), 'A temperature below freezing' assert not above_freezing(0), 'A temperature at freezing' def test_boiling (): '''''Test boiling point''' assert to_celsius(212) == 100 def test_roundoff (): '''''Test that roundoff works''' assert to_celsius(100) == 38, 'Returning an unrounded result' #not 37.77... if __name__ == "__main__": nose.runmodule()
# nose.main() 两种方式运行
命令行运行:nosetests -v -s nose_test3.py
test2,默认会运行setup和teardown
通过修饰器:
from nose.tools.nontrivial import with_setup def setup_func(): print("set up test fixtures") def teardown_func(): print("tear down test fixtures") @with_setup(setup_func, teardown_func) def test(): "test ..."
结果: