一篇文章搞懂Python Unittest测试方法的执行顺序
作者:软件测试君 时间:2023-11-01 09:38:01
Unittest
unittest大家应该都不陌生。它作为一款博主在5-6年前最常用的单元测试框架,现在正被pytest,nose慢慢蚕食。
渐渐地,看到大家更多的讨论的内容从unittest+HTMLTestRunner变为pytest+allure2等后起之秀。
不禁感慨,终究是自己落伍了,跟不上时代的大潮了。
回到主题
感慨完了,回到正文。虽然unittest正在慢慢被放弃,但是它仍然是一款很全面的测试框架。
今天在群里看到有个群友的一番言论,激起了我的一番回忆。
自己以前是知道unittest的执行顺序并不是按照编写test方法的顺序执行,而是按照字典序执行的。但遗憾的是我都是投机取巧去解决的问题(后面会讲)。
下面我们就来探讨下unittest类的test方法的执行顺序问题。
源码初窥
研究一下源码(unittest.TestLoader)可以发现,在加载一个class下面的test方法的时候,原生Loader进行了排序,并且根据functools.cmp_to_key方法对测试方法列表进行了排序。
我们知道,unittest是不需要我们指定对应的方法,说白了,它是从类里面自动获取到咱们的方法,并约定了以test开头的方法都会被视为测试方法。
查询一下self.sortTestMethodsUsing(这个是一个排序的方式)。
可以看到这个比较方法写的很明确了,如果x < y那么返回-1,x = y则返回0,x > y返回1。
其实大家可能不知道Python里面的字符串也是可以比较的,在此必须说明一下字典序。我们来看看这个例子:
a = "abc"
b = "abcd"
c = "abce"
print(a > b)
print(b > c)
猜猜看执行结果,很显然,字典序的比较,是按A-Z的顺序来比较的,如果前缀一样但长度不一样,那么长度长的那个,字典序靠后。
了解了字典序以后,我们就不难知道,在unittest里面它寻找case的过程可以这样简化:
找到对应类下面以test开头的测试方法
对他们进行字典序排序
依次执行
这样就不难解释为什么我们有时候写的case不按照自己想的顺序来。
回到问题的本质
搞清楚为什么用例会乱,那就想到对应的解决方案。由于修改源码是不太合适的,那我们有2个策略去达成目的。
比如我有多个test方法:
class Testcase(unittest.TestCase):
def setUp(self) -> None:
pass
def test_1(self):
print("执行第一个")
def test_2(self):
print("第二个")
def test_3(self):
print("第三个")
def test_10(self):
print("第四个")
def test_11(self):
print("第五个")
def tearDown(self) -> None:
pass
if __name__ == "__main__":
unittest.main()
执行起来,按照字典序,其实是1 10 11 2 3的顺序。
1. 以字典序的方式编写test方法
我们可以手动修改test方法的名称,这也是我早前的处理方式。也就是说把想要先执行的case字典序排到前面:
class Testcase(unittest.TestCase):
def setUp(self) -> None:
pass
def test_0_1(self):
print("执行第一个")
def test_0_2(self):
print("第二个")
def test_0_3(self):
print("第三个")
def test_1_0(self):
print("第四个")
def test_1_1(self):
print("第五个")
def tearDown(self) -> None:
pass
我们可以把数字按位数拆开,个位数就把10位补0,这样就能达到效果,如果会写100个case,我们就需要补2个0,比如0_0_1,当然一个文件里面也不会有太多case。
如果遇到test_login这种怎么办呢,不是数字结尾的方法。
其实是一样的,可以写成test_数字_业务的模式。番货写了一个装饰器专门解决这样的问题,大家可以去参考下。
2. 回归本质,从根本解决问题
方案1用了番货的装饰器,好是好,但是改变了方法本身的名称,我们其实可以针对他的排序方式入手,按照我们编写case的顺序排序测试方法,就能达到想要的目的。
说说思路:
手写一个loader继承自TestLoader类,改写里面的排序方法
在unittest运行的时候传入这个新的loader
来看看完整代码,注释里面写的很完善了。
import unittest
class MyTestLoader(unittest.TestLoader):
def getTestCaseNames(self, testcase_class):
# 调用父类的获取“测试方法”函数
test_names = super().getTestCaseNames(testcase_class)
# 拿到测试方法list
testcase_methods = list(testcase_class.__dict__.keys())
# 根据list的索引对testcase_methods进行排序
test_names.sort(key=testcase_methods.index)
# 返回测试方法名称
return test_names
class Testcase(unittest.TestCase):
def setUp(self) -> None:
pass
def test_1(self):
print("执行第一个")
def test_2(self):
print("第二个")
def test_3(self):
print("第三个")
def test_10(self):
print("第四个")
def test_11(self):
print("第五个")
def tearDown(self) -> None:
pass
if __name__ == "__main__":
unittest.main(testLoader=MyTestLoader())
执行了一下还是不对,是不是哪里出了什么问题呢?
是因为pycharm有一种默认的unittest的调试方法,我们要改成普通的方法去执行。
试试用控制台执行:
来源:https://blog.csdn.net/AI_Green/article/details/120332593
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
node.js+express+mySQL+ejs+bootstrop实现网站登录注册功能
![](https://img.aspxhome.com/file/2023/9/55989_0s.gif)
python 找出list中最大或者最小几个数的索引方法
PHP面向对象程序设计之类与反射API详解
Python使用psutil对系统数据进行采集监控
![](https://img.aspxhome.com/file/2023/2/109842_0s.png)
详解如何基于Pyecharts绘制常见的直角坐标系图表
![](https://img.aspxhome.com/file/2023/4/71424_0s.png)
python爬虫headers设置后无效的解决方法
![](https://img.aspxhome.com/file/2023/9/79609_0s.png)
Python threading中lock的使用详解
sql IDENTITY_INSERT对标识列的作用和使用
![](https://img.aspxhome.com/file/2023/6/84356_0s.png)
Python结巴中文分词工具使用过程中遇到的问题及解决方法
![](https://img.aspxhome.com/file/2023/7/100887_0s.jpg)
js事件委托和事件代理案例分享
python爬虫看看虎牙女主播中谁最“顶”步骤详解
![](https://img.aspxhome.com/file/2023/2/84152_0s.png)
Python实现并行抓取整站40万条房价数据(可更换抓取城市)
![](https://img.aspxhome.com/file/2023/7/76067_0s.png)
Django自定义用户认证示例详解
![](https://img.aspxhome.com/file/2023/5/134495_0s.png)
python异常处理和日志处理方式
MySQL数据库Shell import_table数据导入
![](https://img.aspxhome.com/file/2023/0/104440_0s.png)
如何通过Java监听MySQL数据的变化
![](https://img.aspxhome.com/file/2023/2/100062_0s.jpg)