pycharm中脚本执行的3种模式(unittest框架、pytest框架、普通模式)
背景知识,某次使用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函数试试
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()函数,更不会导出报告
最终通过创建普通模式的运行模式,然后按照如下方式可以运行
最终测试报告如下:
最新文章
- InfoPath错误,此文档库已被重命名或删除
- ArcGIS支持MongoDB数据源
- [U3D 画起重机,绑脚本和控制它运动的基本操作]
- ionic不同view跳转到同一个
- Myeclipse设置JAVA选中高亮显示
- 如何根据iframe内嵌页面调整iframe高宽
- http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/
- Entity Framework学习笔记(六)----使用Lambda查询Entity Framework(1)
- showdialog()与show的区别
- Spring分布式事务实现
- 几种解析xml方式的比较
- QT-Demo-Colck-01
- live555 源代码简单分析1:主程序
- 7.如何发布vue项目到服务器
- Python小代码_12_生成前 n 行杨辉三角
- 玩转web之ligerui(一)---ligerGrid重新指定url
- JZ2440学习笔记之内存设备
- python selenium-webdriver 登录验证码的处理(十二)
- 使用kbmMWConfiguration 让 kbmmw smartservice 更聪明
- Java笔记 #04# 类的初始化顺序补充
热门文章
- 4、重建二叉树------------>;剑指offer系列
- leetcode378 Kth Smallest Element in a Sorted Matrix
- C#实现MD5WITHRSA签名
- Android Studio 遇到的java.util.concurrent.ExecutionException:com.android.ide.common.process.ProcessExce问题
- FPGA的嵌入式RAM
- web前端性能优化 (share)
- MySQL字符集和排序介绍
- CodeForces 219D Choosing Capital for Treeland (树形DP)
- ubuntu 14.04 离线部署docker
- Ubuntu16.04下使用sublime text3搭建Python IDE