zoukankan      html  css  js  c++  java
  • pycharm中脚本执行的3种模式(unittest框架、pytest框架、普通模式)-转载

     转载-https://blog.csdn.net/sinat_38682860/article/details/103597305

    背景知识,某次使用HTMLTestRunner的时候,发现一直都无法导出报告,后来查询资料发现了一些坑,现在整理一下来龙去脉。

    一:pycharm默认的是pytest框架去执行unittest框架的测试用例

    import unittest
    
    class AlienTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            print("TestCase  start running ")
    
        def test_1_run(self):
            print("hello world_1")
    
        def test_2_run(self):
            print("hello world_2")
    
        def test_3_run(self):
            print("hello world_3")

    如上的代码,如果第一次执行,是不会打印任何数据的,最终输出框的代码如下:
    ============================= test session starts ==============================
    platform darwin -- Python 3.6.3, pytest-3.3.1, py-1.5.2, pluggy-0.6.0
    rootdir: /Users/test/The_Order_Of_TestCase/TestCase, inifile:
    collected 3 items
    Alien_Test.py TestCase  start running 
    .hello world_1
    .hello world_2
    .hello world_3   [100%]
    
    
    =========================== 3 passed in 0.13 seconds ===========================

    通过以上信息,正常打印了,但是通过pytest-3.3.1这个框架去执行的测试用例

    现在我们添加3行代码,添加main函数试试

    '''
    遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
    寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
    '''
    import unittest
    
    class AlienTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            print("TestCase  start running ")
    
        def test_1_run(self):
            print("hello world_1")
    
        def test_2_run(self):
            print("hello world_2")
    
        def test_3_run(self):
            print("hello world_3")
    
    
    if __name__ == '__main__':
        print"hello world")
        unittest.main()

    如上代码,我们点击main函数这一行去执行脚本,执行过程如下

    最终我们会发现,结果和第一个步骤是一样的,由此我们得出结论:

    (1)使用pytest测试框架时候,不需要main()函数,系统可以自动识别测试用例并执行。
    (2)即使包含main()函数,点击它去执行,也不会去执行main()函数。
    (3)具体是使用哪个测试框架执行,不是通过main()函数设置的,在别的地方。
    而此时,pycharm右上角的执行框如下所示:

    二:python运行脚本的三种模式

    通过查阅资料才发现,原来python的运行脚本的方式有多种:

    • 例如普通模式运行,不会自动去加载测试用例执行
    • unittest 测试框架运行模式,可以自动去发现testcase并执行
    • pytest 测试框架运行模式,就是我们上面2个步骤都是使用pytest测试框架运行的
    • 重要原则:第一次按照何种模式执行测试用例,后续都会按照这种方式去执行

    因为如上2步,我们都是按照pytest模式去执行的,即使添加的main()函数,最终默认的执行方式都是pytest模式。

    三:如何修改脚本运行的模式呢?

    方法一:修改pycharm默认的测试框架

    具体步骤如下:

    (1)这种方式修改完之后,如果某个文件是第一次运行,那默认执行的测试框架是系统默认的框架
    (2)如果某个文件已经通过其他模式运行了,即使更改了系统默认测试框架,也还是按照第一次运行的模式去执行。

    方法二:设置运动脚本时候的默认框架

    入口一:菜单栏Run—->Run—->Edit Configuration

    入口二:右上角运行按钮旁边—>Edit Configurations

    在这里插入图片描述

    通过以上2种入口,进入设置页面如下所示:


    在这里插入图片描述

    上图中,左边的方框运行的内容在Python tests栏目下面,说明脚本已经使用pytest测试框架运行了,但我们系统TestCase是通过Unittest框架运行的

    我们需要添加unittest框架的运行模式去运行脚本,具体步骤如下:
    在这里插入图片描述
    在这里插入图片描述
    最终效果如下:
    在这里插入图片描述

    此时,我们再去执行脚本(点击main()运行或者点击右上角按钮运行结果是一样的)
    在这里插入图片描述

    结果如下:

    TestCase start running hello world_1
    hello world_2
    hello world_3

    • 如上的结果说明调用的unittest框架去执行,但是没有调用main()函数。
    • 如果单纯为了执行测试用例,不用写main()函数也可以

    四:main()函数有啥作用,难道就没点价值吗?

    场景一:执行单个测试用例(删除下图右上角脚本运行的记录了)


    在这里插入图片描述
    在这里插入图片描述

    结果如下:

    TestCase start running
    hello world_2

    说明只执行了一个测试用例,test_2_run

    场景二:执行所有测试用例(删除下图右上角脚本运行的记录了)

    最终结果如下:

    TestCase  start running 
    hello world_1
    hello world_2
    hello world_3

    执行所有测试用例的方法:

    其实只要不点击单个测试用例的定义函数的行,无论点击哪一行,即使点击main()函数一行,或者空白行,都可以执行所有测试用例

    如果代码比较多的情况加,我们又想尽快执行所有的测试用例,点击main()函数可以执行所以的测试用例,不会出错,目前能想到的就这些……

    五:普通模式运行测试用例(删除下图右上角脚本运行的记录了)

    在这里插入图片描述

    在这里插入图片描述

    结果:

    hello world
    TestCase  start running 
    hello world_1
    hello world_2
    hello world_3

    使用普通模式运行,系统会运行main()函数里面所有的函数,包括非TestCase的函数,当main()函数中有测试报告的需要导出的时候,需要使用普通模式运行。

    使用pytest或unittest测试框架,在main函数中只执行TestCase函数,其他函数不执行。

    六:普通运行模式,导出测试报告

    import unittest
    import time,HTMLTestRunner
    
    
    class AlienTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            print("TestCase  start running ")
    
        def test_1_run(self):
            print("hello world_1")
    
        def test_2_run(self):
            print("hello world_2")
    
        def test_3_run(self):
            print("hello world_3")
    
    
    if __name__ == '__main__':
        print('hello world')
        suite = unittest.makeSuite(AlienTest)
        now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime())
        filename = "/Users/test/The_Order_Of_TestCase/Report/" + now + "_result.html"
        fp = open(filename, 'wb')
        runner = HTMLTestRunner.HTMLTestRunner(
            stream=fp,
            title=u'ALIEN测试报告',
            description=u'ALIEN用例执行情况:')
    
        runner.run(suite)
        # 关闭文件流,不关的话生成的报告是空的
        fp.close()

    默认是使用unittest模式运行的,结果如下,其实这样不会执行main()函数,更不会导出报告
    在这里插入图片描述

    最终通过创建普通模式的运行模式,然后按照如下方式可以运行

    在这里插入图片描述

    最终测试报告如下:

    在这里插入图片描述

     
     
  • 相关阅读:
    [转]oracle 12c 中的分页子句
    display:table; 也可以实现 div 始终和内包的图片大小相同
    mongodb与SQL常见语句对照
    js生成验证码
    javascript 判断空数组
    网络绘制插件
    mongodb中查询返回指定字段
    Mybatis获取插入记录的自增长ID
    mysql 创建索引和删除索引
    Java 中 HashMap 初始化时赋值
  • 原文地址:https://www.cnblogs.com/yiyaxuan/p/14777275.html
Copyright © 2011-2022 走看看