import unittest
class Test_01(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""
类级别
"""
print("类开始")
def setUp(self):
"""
函数级别
"""
print("函数开始")
def test_01(self):
print("我是测试test_01模块中的:test_01")
def test_02(self):
print("我是测试test_01模块中的:test_02")
def tearDown(self):
"""
函数级别
"""
print("函数结束")
@classmethod
def tearDownClass(cls):
"""
类级别
"""
print("类结束")
if __name__ == '__main__':
unittest.main()
运行结果:
类开始
..
函数开始
我是测试test_01模块中的:test_01
函数结束
----------------------------------------------------------------------
函数开始
Ran 2 tests in 0.000s
OK
我是测试test_01模块中的:test_02
函数结束
类结束
import unittest
class T1(unittest.TestCase):
def test_01(self):
print("我是T1类中的test_01")
def test_02(self):
print("我是T1类中的test_02")
class T2(unittest.TestCase):
def test_01(self):
print("我是T2类中的test_01")
if __name__ == '__main__':
suite = unittest.TestSuite()
# 类名(函数名)--指定加载函数
suite.addTest(T2("test_01"))
# 类名--加载类中的所有测试函数
suite.addTest(unittest.makeSuite(T1))
runner = unittest.TextTestRunner()
runner.run(suite)
运行结果:
我是T2类中的test_01
...
我是T1类中的test_01
我是T1类中的test_02
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
import unittest
class T1(unittest.TestCase):
def test_01(self):
print("我是T1类中的test_01")
def test_02(self):
print("我是T1类中的test_02")
class T2(unittest.TestCase):
def test_01(self):
print("我是T2类中的test_01")
import unittest
if __name__ == '__main__':
# start_dir : 测试用例所在的文件夹
# pattern:匹配模式,默认 test*.py
# suite = unittest.TestLoader().discover(start_dir="./scripts", pattern="test*.py")
suite = unittest.defaultTestLoader.discover(start_dir="./scripts", pattern="test*.py")
runner = unittest.TextTestRunner()
runner.run(suite)
运行结果:
我是T1类中的test_01
我是T1类中的test_02
我是T2类中的test_01
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
import unittest
class T1(unittest.TestCase):
def test_01(self):
# msg 不成立则打印
self.assertTrue(True, msg="不是True")
self.assertFalse(False, msg="不是False")
self.assertEqual(1, 1, msg="两个比较数不相等")
self.assertNotEqual(1, 2, msg="两个比较数相等")
self.assertIsNone(None, msg="不为None")
self.assertIsNotNone("123", msg="为None")
# 断言"ab" 在"dafjabc"中
self.assertIn("ab", "dafjabc", msg="在字符串中")
# 断言"abcd" 不在"dafjabc"中
self.assertNotIn("abcd", "dafjabc", msg="不字符串中")
if __name__ == '__main__':
unittest.main()
pip install parameterized
from parameterized import parameterized
import unittest
class T1(unittest.TestCase):
def setUp(self):
print("函数开始")
@parameterized.expand([(1, 2, 3), (11, 22, 33), (111, 222, 333)])
def test(self, a, b, c):
print(a, b, c)
if __name__ == '__main__':
unittest.main()
函数开始
1 2 3
函数开始
11 22 33
函数开始
111 222 333
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
import unittest
class T1(unittest.TestCase):
temp = 10
def test(self):
print("test--->")
@unittest.skip("暂时不执行该函数")
def test1(self):
print("test1--->")
# True,则跳过,第一个参数bool值,第二个说明
@unittest.skipIf(temp > 2, "temp大于2时跳过")
def test2(self):
print("test2--->")
# False 跳过
@unittest.skipUnless(temp > 2, "temp小于2时跳过")
def test3(self):
print("test3--->")
if __name__ == '__main__':
unittest.main()
运行结果:
.ss.
test--->
----------------------------------------------------------------------
Ran 4 tests in 0.000s
test3--->
OK (skipped=2)
import unittest
class T1(unittest.TestCase):
def test_01(self):
print("我是T1类中的test_01")
def test_02(self):
print("我是T1类中的test_02")
class T2(unittest.TestCase):
def test_01(self):
print("我是T2类中的test_01")
import unittest
from BeautifulReport import BeautifulReport
if __name__ == '__main__':
suite = unittest.defaultTestLoader.discover(start_dir="./scripts",pattern="test*.py")
result = BeautifulReport(suite)
# filename -> 测试报告名称, 如果不指定默认文件名为report.html
# description -> 测试报告用例名称展示
# log_path='.' -> log文件写入路径
result.report(filename="测试报告",description="测试deafult报告",log_path='report')
import unittest
import time
from BeautifulReport import BeautifulReport
from selenium import webdriver
class T3(unittest.TestCase):
def test_01(self):
"""
我是test_01函数
"""
print("我是T3中的test_01函数")
def test_02(self):
"""
我是test_02函数
"""
print("我是T3中的test_02函数")
# 断言失败,信息收集在html的展开中
self.assertEqual(1,2)
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(T3("test_01"))
suite.addTest(T3("test_02"))
result = BeautifulReport(suite)
result.report(filename="测试报告",description="测试deafult报告",log_path='report')
运行结果:
import unittest
from selenium import webdriver
from BeautifulReport import BeautifulReport
class T3(unittest.TestCase):
""" 测试T3"""
def save_img(self, img_name):
"""
保存图片
"""
self.driver.get_screenshot_as_file('{}/{}.png'.format("./img", img_name))
@BeautifulReport.add_test_img('百度首页')
def test_01(self):
"""
打开首页, 截图
"""
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
# 断言失败
self.assertEqual(1,2)
self.driver.quit()
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(T3("test_01"))
result = BeautifulReport(suite)
result.report(filename="测试报告", description="测试deafult报告", log_path='report')
import unittest
from selenium import webdriver
from BeautifulReport import BeautifulReport
class T3(unittest.TestCase):
def save_img(self, img_name):
"""
保存图片
"""
self.driver.get_screenshot_as_file('{}/{}.png'.format("./img", img_name))
@BeautifulReport.add_test_img('百度首页', "百度首页点击新闻按钮")
def test_01(self):
"""
打开首页截图,点击新闻链接截图
"""
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
self.save_img('百度首页')
self.driver.find_element_by_xpath("//*[@id='u1']/a[1]")
self.save_img('百度首页点击新闻按钮')
print('跳转与保存截图完成')
self.driver.quit()
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(T3("test_01"))
result = BeautifulReport(suite)
result.report(filename="测试报告", description="测试deafult报告", log_path='report')