python seleium运行成功但是没有测试报告生成

生成测试报告需要下载:

HTMLTestRunner

源地址:http://tungwaiyip.info/software/HTMLTestRunner.html
这是针对Python2.7版本,那么对于Python3.x的使用,需要改动几处。同时谢谢 http://www.bubuko.com/infodetail-529431.html的分享。
具体改动如下:
第94行,将import StringIO修改成import io
第539行,将self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer= io.StringIO()
第631行,将print >> sys.stderr, ‘\nTime Elapsed: %s‘ %(self.stopTime-self.startTime)修改成print(sys.stderr, ‘\nTimeElapsed: %s‘ % (self.stopTime-self.startTime))
第642行,将if not rmap.has_key(cls):修改成if notcls in rmap:
第766行,将uo = o.decode(‘latin-1‘)修改成uo = e
第775行,将ue = e.decode(‘latin-1‘)修改成ue = e
第778行,将output = saxutils.escape(uo+ue),修改成output = saxutils.escape(str(uo)+str(ue)),

将上述几处改动,保存成功后,再将HTMLTestRunner.py放到C:\Python34\Lib目录中,检验是否加载成功,在Python IDLE 中输入
import HTMLTestRunner
若无报错,那么加载成功。

 首先,这个方法是针对那些运行成功,显示OK,但是没有文档生成的问题,如果运行报错的同学,那可以默默的关上文章,因为本文非针对报错进行解答。如果不是的小伙伴可以继续往下看。 

   下面是我的代码:

 

# -*- coding: utf-8 -*-
from HTMLTestRunner import HTMLTestRunner
import time
import unittest
from selenium import webdriver


class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        # self.base_url = "https://www.baidu.com"
        # self.driver.get(self.base_url)
        self.driver.get("https://www.baidu.com")

    def test_case1(self):
        """设计测试失败case"""  # *****效果是在测试报告中显示显示出测试名称*****
        print("========【case_0001】打开百度搜索 =============")
        # current_time = time.strftime("%Y-%M-%D-%H-%M-%S", time.localtime(time.time()))
        # "."表示创建的路径为当.py文件所处的地址,\\是用\将“\”转义
        # pic_path = '.\\result\\image\\' + current_time + '.png'
        current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
        pic_path = '.\\result\\image\\' + '2017-07-17\\' + current_time + '.png'
        print(pic_path)  # 打印图片的地址
        time.sleep(2)
        self.driver.save_screenshot(pic_path)  # 截图,获取测试结果
        self.assertEqual('百度一下,你就知道', self.driver.title)  # 断言判断测试是否成功,判断标题是否为百度(设计失败的case)
        print("==============第一次=====================")
    def test_case2(self):
        """设计测试过程中报错的case"""
        print("========【case_0002】搜索selenium =============")
        self.driver.find_element_by_id("kw").clear()
        self.driver.find_element_by_id("kw").send_keys(u"selenium")
        self.driver.find_element_by_id('su').click()
        time.sleep(2)
        # current_time = time.strftime("%Y-%M-%D-%H-%M-%S", time.localtime(time.time()))
        current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
        # "."表示创建的路径为当.py文件所处的地址,\\是用\将“\”转义
        pic_path = '.\\result\\image\\'+'2017-07-17\\' + current_time + '.png'
        print(pic_path)  # 打印图片的地址
        time.sleep(2)
        self.driver.save_screenshot(pic_path)  # 截图,获取测试结果
        self.assertIn('selenium', self.driver.title)  # 断言书写错误,导致case出错
        print("=====================第二次======================")
    def tearDown(self):
        self.driver.quit()


if __name__ == "__main__":
    '''生成测试报告'''
    current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
    testunit = unittest.TestSuite() # 定义一个单元测试容器
    testunit.addTest(Baidu("test_case1")) #将测试用例加入到测试容器内
    testunit.addTest(Baidu("test_case2"))
    report_path = "./testHtml" + current_time + 'abc.html'  # 生成测试报告的路径
    fp = open(report_path, "wb")
    runner =HTMLTestRunner(stream=fp, title=u"自动化测试报告", description='自动化测试演示报告')
    # runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description='自动化测试演示报告')
    runner.run(testunit)
    fp.close()

右键单击执行python seleium运行成功但是没有测试报告生成

运行成功但是没有testHtml测试报告生成

  网上搜索,发现反映类似的问题超级多,很多回答都是超级不靠谱,甚至有些奇葩。其中有一种方法我试过是可行的,就是在所在文件夹下,cmd运行,可以生成。但是我们不可能每次都去用这种方法去运行啊,诚麻烦啊(我用的是pycharm编辑的)。

      后来我想了一下,是不是本身的配置有问题,既然文件没在我想要的文件夹下生成,那是不是运行的路径不对。因为每次都是编辑后
python seleium运行成功但是没有测试报告生成

python seleium运行成功但是没有测试报告生成

发现运行的是unittest,并没有运行那个我们想要run,只是运行了框架里面的

把Python tests里的edit为Python中,然后把Script path选择为运行文件的路径,点击运行按钮,期待已久的result.html文件终于出来了,特此记录一下。

python seleium运行成功但是没有测试报告生成

这个方法目前我还没试过,上面所述方法还行,有兴趣的同学还可以学习一下