【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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值