【Python+unittest】测试套件TestSuite执行完unittest测试用例后,TextTestRunner打印生成TXT格式的测试报告

根据之前的一段代码,生成运行后的测试报告。

https://blog.csdn.net/woshiyigerenlaide/article/details/104117384

文件名称:Calculator.py。一段用python编写的计算器Calculator代码

def add(x,y):
    return x + y
def sub(x,y):
    return x - y

文件名称:test36.py 。这是单元测试代码:测试用例代码

# _*_ coding: utf-8 _*_
from unittest import TestCase, main, skip
from test35 import add, sub

x, y = 5, 3
class MyTestCase(TestCase):
    def setUp(self):
        pass
    def test_01(self):
        result1 = add(x, y)
        self.assertEqual(result1, 8, "add方法,执行结果不正确")
    @skip("stop test_02")
    def test_02(self):
        print('test-02')

    # test_03的断言刻意写错了,让大家可以看看执行结果
    def test_03(self):
        result2 = sub(x, y)
        self.assertEqual(result2, 22, "sub方法,执行结果不正确")

    def tearDown(self):
        pass

if __name__ == '__main__':
    main()

这是测试套件的代码:(这段代码生成测试报告)

# _*_ coding: utf-8 _*_
from test36 import MyTestCase
from unittest import TestSuite, TextTestRunner
suite = TestSuite()
 
suite.addTest(MyTestCase('test_01'))
suite.addTest(MyTestCase('test_02'))
suite.addTest(MyTestCase('test_03'))
 
if __name__ == '__main__':
# 生成测试报告
    with open("result.txt","w+") as f:
        runner = TextTestRunner(stream = f,verbosity = 2)
        runner.run(suite)

生成了result.txt。

test_01 (test36.MyTestCase) ... ok
test_02 (test36.MyTestCase) ... skipped 'stop test_02'
test_03 (test36.MyTestCase) ... FAIL

======================================================================
FAIL: test_03 (test36.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\018410\PycharmProjects\Selenium\test36.py", line 19, in test_03
    self.assertEqual(result2, 22, "sub方法,执行结果不正确")
AssertionError: sub方法,执行结果不正确

----------------------------------------------------------------------
Ran 3 tests in 0.003s

FAILED (failures=1, skipped=1)

OK
发布了94 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/woshiyigerenlaide/article/details/104188120