python自动化测试,将测试结果的报告写入本地中(HTMLTestRunner)
1、首先环境python3
2、其次需要安装HTMLTestRunner模块,涉及的文件
由于会涉及到乱码,因此直接将下方的文件放入python安装的地方:lib:放入当中
链接:https://pan.baidu.com/s/1DXF2abBLl3YcD8pTIv41vg
提取码:RHHS
3、下面涉及代码与操作
(1)main.py
(2)写好的代码放入此处:
需要注意:名字需要以test开头,因为此处的自动化测试,跑任务的时候都是直接根据test进行判断的(可以试试如果不是test开头的文件名,就不会运行)
(3)直接运行main.py
最后得到的报告就会在此处。
使用浏览器打开 即此处为自动化测试报告
之后可以直接在机器上设置定时任务,然后定时任务将报告发送至邮箱。
代码如下:
main.py:
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import unittest
import HTMLTestRunner
# def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
# print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
import time
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
testcase_dir=r'D:\\自动化'
# print_hi('PyCharm')
print(testcase_dir)
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
suite = unittest.defaultTestLoader.discover(testcase_dir,'test_*.py')
timestr = time.strftime('%Y%m%d',time.localtime(time.time()))
filename = "D:\\自动化测试报告"+timestr+".html"
print(filename)
fp = open(filename,'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='自动化测试报告', description='用例执行情况')
runner.run(suite)
fp.close()
test_luzhi.py:
# Generated by Selenium IDE
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import unittest
class TestUntitled(unittest.TestCase):
def setup_method(self):
self.driver = webdriver.Chrome()
self.vars = {}
def teardown_method(self):
self.driver.quit()
def test_untitled(self):
self.setup_method()
time.sleep(1)
self.driver.get("https://www.baidu.com/")
self.driver.set_window_size(1363, 1003)
self.driver.find_element(By.ID, "kw").click()
self.driver.find_element(By.ID, "kw").send_keys("自动化测试")
self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)
time.sleep(5)