为什么python+htmltestrunner生成的测试报告有问题?

 Edward2502873903 发布于 2022-10-25 17:47

问题:一个百度首页搜索的一个python+unittest测试,代码执行成功,但是用HTMLTestRunner输出的测试报告里面得内容有问题,具体问题是:测试条数,成功数,失败数都为0
代码

# -*- coding: utf-8 -*-
from selenium import webdriver
import os
import time
import unittest
import re
import HTMLTestRunner
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException

class LoginBaiDu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.baidu.com"
        self.verificationErrors = []
        self.accept_next_alert = True
        print('Done-01')
        
    def test_baidu(self):
        self.driver.get(self.base_url)
        self.driver.find_element_by_id("su").click()
        print('Done-02')
        time.sleep(2)
        jsClear="$(\"input[id='kw']\").val(\"\")"
        self.driver.execute_script(jsClear)
        print('Done-03')
        time.sleep(2)
        jsVal="$(\"input[id='kw']\").val(\"selenium+python\")"
        self.driver.execute_script(jsVal)
        time.sleep(1)
        self.driver.find_element_by_xpath("//input[@id='su']").click()
        print('Done-04')
        self.driver.close()
        print('Done-05')
        
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
        print("test down...")
        
    if __name__=="__main__":
        test=unittest.TestSuite()
        
        test.addTest(setUp)
        test.addTest(test_baidu)
        
        
        file_path="D:\\workspace\\PythonLearn\\src\\LoginBaiDu\\result.html"
        file_result=open(file_path,'wb')
        
        runner=HTMLTestRunner.HTMLTestRunner(stream=file_result,title=u"百度首页测试",description=u"用例执行情况")
        
        runner.run(test)
        file_result.close()     
        

生成的报告截图:

1 个回答
  • 加测试用例的方式错了要
    test=unittest.TestSuite()
    test.addTest(LoginBaiDu('setUp'))
    test.addTest(LoginBaiDu('test_baidu'))

    或者直接加入类
    test= unittest.TestLoader().loadTestsFromTestCase(LoginBaiDu)

    2022-10-26 23:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有