|
| 1 | +#coding: utf8 |
| 2 | +""" |
| 3 | + pip install nose |
| 4 | + |
| 5 | + 运行测试: |
| 6 | + nosetests |
| 7 | + 选项: |
| 8 | + -w ,指定一个目录运行测试。目录可以是相对路径或绝对路径。 |
| 9 | + -s,不捕获输出,会让你的程序里面的一些命令行上的输出显示出来。例如print所输出的内容。 |
| 10 | + -v,查看nose的运行信息和调试信息。例如会给出当前正在运行哪个测试。 |
| 11 | + |
| 12 | + nose在文件中如果找到函数setup, setup_module, setUp 或者setUpModule等, |
| 13 | + 那么会在该模块的所有测试执行之前执行该函数。 |
| 14 | + 如果找到函数 teardown,tearDown, teardown_module或者 tearDownModule等, |
| 15 | + 那么会在该模块所有的测试执行完之后执行该函数。 |
| 16 | +""" |
| 17 | +from nose.tools import with_setup |
| 18 | +import nose |
| 19 | + |
| 20 | +def setUp(): |
| 21 | + print "function setup" |
| 22 | + |
| 23 | + |
| 24 | +def tearDown(): |
| 25 | + print "function teardown" |
| 26 | + |
| 27 | + |
| 28 | +def TestFunc1(): |
| 29 | + print "Test func1" |
| 30 | + assert True |
| 31 | + |
| 32 | + |
| 33 | +def TestFunc2(): |
| 34 | + print "Test func2" |
| 35 | + assert True |
| 36 | + |
| 37 | + |
| 38 | +def func3Start(): |
| 39 | + print "Func3 start" |
| 40 | + assert True |
| 41 | + |
| 42 | + |
| 43 | +def func3End(): |
| 44 | + print "Func3 end" |
| 45 | + assert True |
| 46 | + |
| 47 | + |
| 48 | +@with_setup(func3Start, func3End) |
| 49 | +def TestFunc3(): |
| 50 | + print "Test func3" |
| 51 | + assert True |
| 52 | + |
| 53 | + |
| 54 | +class TestClass(): |
| 55 | + arr1 = 2 |
| 56 | + arr2 = 2 |
| 57 | + |
| 58 | + def setUp(self): |
| 59 | + self.arr1 = 1 |
| 60 | + self.arr2 = 3 |
| 61 | + print "MyTestClass setup" |
| 62 | + |
| 63 | + def tearDown(self): |
| 64 | + print "MyTestClass teardown" |
| 65 | + |
| 66 | + def TestFunc4(self): |
| 67 | + assert self.arr1 != self.arr2 |
| 68 | + |
| 69 | + def TestFunc5(self): |
| 70 | + assert self.arr1 == 1 |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + nose.runmodule(argv=[__file__, '--with-doctest', '-vv']) |
0 commit comments