import unittest
# import HTMLTestRunner # 难看的测试报告
from BeautifulReport import BeautifulReport # 漂亮的测试报告
'''
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
'''
def calc(x, y):
return x + y
class TestCala(unittest.TestCase):
def test_pass_case(self):
"""通过的测试用例"""
print('这个是通过的用例')
res = calc(1, 2)
self.assertEqual(3, res)
def setUp(self): # 每个用例运行之前都会执行它
print('我是setup')
def tearDown(self): # 每个用例运行之后都会执行它
print('我是teardown')
@classmethod
def setUpClass(cls): # 所有的用例执行之前运行一次
print('我是setupclass')
@classmethod
def tearDownClass(cls): # 所有的用例执行之后运行一次
print('我是teardownclass')
def test_fail_case(self):
"""失败的测试用例"""
print('这个是失败的测试用例')
res = calc(9, 8)
self.assertEqual(98, res)
def test_a_case(self):
"""通过的测试用例"""
print('哈哈哈哈')
def test_haha(self):
"""哈啊哈"""
self.assertEqual(1, 2)
if __name__ == '__main__':
# unittest.main() # 他会帮你运行当前这个python里面所有的测试用例
suite = unittest.TestSuite() # 定义一个测试套件
# 单个添加用例的
suite.addTest(TestCala('test_pass_case'))
suite.addTest(TestCala('test_fail_case'))
suite.addTest(TestCala('test_a_case'))
# 这个类里面的所有测试用例
# suite.addTests(unittest.makeSuite(TestCala))
# 下面注释是难看的测试报告
# f = open('report.html', 'wb') # 打开一个测试报告的文件
# runner = HTMLTestRunner.HTMLTestRunner(stream=f, title='王思磊测试结果', description='描述')
# runner.run(suite) # 运行
result = BeautifulReport(suite)
result.report(filename='王思磊测试报告', description='描述', log_path='.')
遍历py文件夹下每个文件
以及给jenkins的
import unittest
from BeautifulReport import BeautifulReport
import xmlrunner
suite = unittest.TestSuite()
all_case = unittest.defaultTestLoader.discover('cases', 'test_*.py')
# for case in all_case:
# suite.addTests(case)
[suite.addTests(case) for case in all_case]
# result = BeautifulReport(suite)
# result.report(filename='王思磊测试报告很多的', description='描述', log_path='.')
runner = xmlrunner.XMLTestRunner('.') # 目录地址
runner.run(suite) # 为了以后给Jenkins看