网络异常,图片无法展示
|
用例组织
在单元测试中,通常我们会将用例按照项目,测试套,测试用例这个层次进行划分,在 nose
中怎么组织用例结构呢?
网络异常,图片无法展示
|
如上图所示:
- 第一层级:测试项目(noses_project)
- 第二层级:测试套件(test_num&test_str)
- 第三层级:用例集合(test_01&test_02)
入口文件:main.py
用例集合中可以进行用例的编写。
test_01.py
def test_A(): assert 1+1 == 2 def test_B(): assert 2-1 == 1 复制代码
test_02.py
def test_1(): assert 'a' == 'a' def test_2(): assert 'A' == 'a' 复制代码
main.py
import os ph = os.path.dirname(__file__) cmd = 'nosetests -v {}'.format(ph) if __name__ == "__main__": os.system(cmd) 复制代码
执行测试:
python main.py test_01.test_A ... ok test_01.test_B ... ok test_02.test_1 ... ok test_02.test_2 ... FAIL ====================================================================== FAIL: test_02.test_2 ---------------------------------------------------------------------- Traceback (most recent call last): File "d:\python37\lib\site-packages\nose\case.py", line 198, in runTest self.test(*self.arg) File "c:\Users\Administrator\Desktop\document\wechatPublic\noses_project\test_str\test_02.py", line 5, in test_2 assert 'A' == 'a' AssertionError ---------------------------------------------------------------------- Ran 4 tests in 0.041s FAILED (failures=1) 复制代码
根据测试结果,我们可以知道在测试集合 test_01
中的两个测试用例 test_A
和 test_B
均通过,而测试集合 test_02
中的两个测试用例 test_1
通过,但是 test_2
未通过,因为断言失败。
整体项目的测试通过率为:3/4
。
setup 和 teardown
setup
和 teardown
通常在用例开始前后执行,在 nose
中 setup
在用例集开始前执行,teardown
在用例集结束后执行。
test_01.py
def setup(): print('tests set 01 will be start...') def teardown(): print('tests set 01 has been end...') def test_A(): assert 1+1 == 2 def test_B(): assert 2-1 == 1 复制代码
test_02.py
def setup(): print('tests set 02 will be start...') def teardown(): print('tests set 02 has been end...') def test_1(): assert 'a' == 'a' def test_2(): assert 'A' == 'a' 复制代码
main.py
import os ph = os.path.dirname(__file__) cmd = 'nosetests -v -s {}'.format(ph) if __name__ == "__main__": os.system(cmd) 复制代码
执行测试:
python main.py tests set 01 will be start... test_01.test_A ... ok test_01.test_B ... ok tests set 01 has been end... tests set 02 will be start... test_02.test_1 ... ok test_02.test_2 ... FAIL tests set 02 has been end... ====================================================================== FAIL: test_02.test_2 ---------------------------------------------------------------------- Traceback (most recent call last): File "d:\python37\lib\site-packages\nose\case.py", line 198, in runTest self.test(*self.arg) File "c:\Users\Administrator\Desktop\document\wechatPublic\noses_project\test_str\test_02.py", line 11, in test_2 assert 'A' == 'a' AssertionError ---------------------------------------------------------------------- Ran 4 tests in 0.036s FAILED (failures=1) 复制代码
注意:
我们除了在用例集文件中加了 setup
和 teardown
方法以外,我们还在 main
文件中增加了-s
参数,用例捕获测试过程中的输出并打印,即捕获 print
的内容并打印出来。
测试报告
你可能会想,这么轻量化的框架是不是没有测试报告?不,答案是有的,nose
为我们提供了 xml
格式的测试报告,方便我们后续解析进行二次开发使用。
使用参数:--with-xunit
main.py
import os ph = os.path.dirname(__file__) cmd = 'nosetests -v -s --with-xunit {}'.format(ph) if __name__ == "__main__": os.system(cmd) 复制代码
执行测试:
python main.py tests set 01 will be start... test_01.test_A ... ok test_01.test_B ... ok tests set 01 has been end... tests set 02 will be start... test_02.test_1 ... ok test_02.test_2 ... FAIL tests set 02 has been end... ====================================================================== FAIL: test_02.test_2 ---------------------------------------------------------------------- Traceback (most recent call last): File "d:\python37\lib\site-packages\nose\case.py", line 198, in runTest self.test(*self.arg) File "c:\Users\Administrator\Desktop\document\wechatPublic\noses_project\test_str\test_02.py", line 11, in test_2 assert 'A' == 'a' AssertionError ---------------------------------------------------------------------- XML: C:\Users\Administrator\Desktop\document\wechatPublic\noses_project\nosetests.xml ---------------------------------------------------------------------- Ran 4 tests in 0.031s FAILED (failures=1) 复制代码
输出的 nosetests.xml
文件:
网络异常,图片无法展示
|
nosetests.xml
<?xml version="1.0" encoding="UTF-8"?><testsuite name="nosetests" tests="4" errors="0" failures="1" skip="0"><testcase classname="test_01" name="test_A" time="0.000"></testcase><testcase classname="test_01" name="test_B" time="0.000"></testcase><testcase classname="test_02" name="test_1" time="0.000"></testcase><testcase classname="test_02" name="test_2" time="0.000"><failure type="builtins.AssertionError" message=""><![CDATA[Traceback (most recent call last): File "d:\python37\lib\unittest\case.py", line 59, in testPartExecutor yield File "d:\python37\lib\unittest\case.py", line 615, in run testMethod() File "d:\python37\lib\site-packages\nose\case.py", line 198, in runTest self.test(*self.arg) File "c:\Users\Administrator\Desktop\document\wechatPublic\noses_project\test_str\test_02.py", line 11, in test_2 assert 'A' == 'a' AssertionError ]]></failure></testcase></testsuite>