‘‘‘ 时间:2018/12/06 功能:html报告 目录: 一: 工程结构 二: 测试报告 - 一般 三: 测试报告 - HTMLTestRunner ‘‘‘
一: 工程结构
1 : 一级目录 : 工程名称 - web_auto_test
2 : 二级目录 : 测试用例 - case
3 : 二级目录 : 公共模块 - common
4 : 二级目录 : 测试报告 - report
5 : 二级文件 : 运行文件 - run_all.py
# coding:utf-8 import unittest class IntegerArithmeticTestCase(unittest.TestCase): def testAdd(self): # test method names begin with ‘test‘ self.assertEqual((1 + 2), 3) self.assertEqual(0 + 1, 1) def testMultiply(self): self.assertEqual((0 * 10), 0) self.assertEqual((5 * 8), 40) if __name__ == ‘__main__‘: unittest.main()
1 : test001_case_count.py
# coding:utf-8 from selenium import webdriver import time import unittest class LoginTest(unittest.TestCase): ‘‘‘登录类的案例‘‘‘ @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() @classmethod def tearDownClass(cls): cls.driver.quit() # 编辑器问题 def setUp(self): self.driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html") def tearDown(self): self.is_alert_exist() self.driver.delete_all_cookies() # 退出登录 self.driver.refresh() def get_login_username(self): try: t = self.driver.find_element_by_css_selector("#userMenu>a").text print(t) return t except: return "" def is_alert_exist(self): ‘‘‘判断alert是不是在‘‘‘ try: time.sleep(2) alert = self.driver.switch_to.alert text = alert.text alert.accept() # 用alert 点alert return text except: return "" def test_01(self): ‘‘‘登录成功的案例‘‘‘ time.sleep(2) self.driver.find_element_by_id("account").send_keys("admin") self.driver.find_element_by_name("password").send_keys("123456") self.driver.find_element_by_id("submit").click() # 判断是否登陆成功 time.sleep(3) t = self.get_login_username() print("获取的结果:%s"%t) self.assertTrue("admin" == t) def test_02(self): ‘‘‘登录失败的案例‘‘‘ time.sleep(2) # 错误账号和密码 self.driver.find_element_by_id("account").send_keys("admin1112") self.driver.find_element_by_name("password").send_keys("") self.driver.find_element_by_id("submit").click() # 判断是否登陆成功 time.sleep(3) t = self.get_login_username() print("登录失败,获取结果:%s"%t) # self.assertTrue("" == t) self.assertTrue(False == True) # 断言失败截图 if __name__ == "__main__": unittest.main()
1 : test002_case_login_chandao.py
二: 测试报告 - 普通
# coding:utf-8 import unittest import os # 路径兼容 - windows/mac cur_path = os.path.dirname(os.path.realpath(__file__)) # 获取路径 - 当前脚本 cur_path = os.path.join(cur_path, "case") # 拼接路径 # 显示结果 - unnitest pattern = "test*.py" # 匹配文件 discover = unittest.defaultTestLoader.discover(start_dir = cur_path, pattern = pattern) print(discover) runner = unittest.TextTestRunner() runner.run(discover)
D:\ProgramTools\Python\python.exe "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/web_auto_test/run_all.py" ..<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<test001_case_count.IntegerArithmeticTestCase testMethod=testAdd>, <test001_case_count.IntegerArithmeticTestCase testMethod=testMultiply>]>]>, <unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<test002_case_login_chandao.LoginTest testMethod=test_01>, <test002_case_login_chandao.LoginTest testMethod=test_02>]>]>]> admin 获取的结果:admin .登录失败,获取结果: F ====================================================================== FAIL: test_02 (test002_case_login_chandao.LoginTest) 登录失败的案例 ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\ProgramTools\PyCharm 5.0.4\PycharmProject\web_auto_test\case\test002_case_login_chandao.py", line 67, in test_02 self.assertTrue(False == True) # 断言失败截图 AssertionError: False is not true ---------------------------------------------------------------------- Ran 4 tests in 24.103s FAILED (failures=1) Process finished with exit code 0
三: 测试报告 - HTMLTestRunner
# coding:utf-8 import unittest import os from common import HTMLTestRunner_cn # 路径兼容 - windows/mac cur_path = os.path.dirname(os.path.realpath(__file__)) # 获取路径 - 当前脚本 cur_path = os.path.join(cur_path, "case") # 拼接路径 # 显示结果 - unnitest pattern = "test*.py" # 匹配文件 discover = unittest.defaultTestLoader.discover(start_dir = cur_path, pattern = pattern) print(discover) # 路径兼容 - windows/mac cur_path = os.path.dirname(os.path.realpath(__file__)) # 获取路径 - 当前脚本 report_path = os.path.join(cur_path, "report", "report.html") # 显示结果 - HTMLTestRunner fp = open(report_path, "wb") runner = HTMLTestRunner_cn.HTMLTestRunner(stream = fp, # 报告路径 title = "接口测试用例报告", # 报告标题 description = "测试用例详情报告", # 报告描述 retry = 1, # 错误重写执行 verbosity = 2) # 注释显示 runner.run(discover) fp.close()
1 : run_all.py
1 : 全部运行通过的测试报告
1 : 运行失败的测试报告
原文地址:https://www.cnblogs.com/huafan/p/10079543.html
时间: 2024-10-11 23:14:09