how to specify test specific setup and teardown in python unittest(如何在python unittest中指定特定于测试的设置和拆卸)
本文介绍了如何在python unittest中指定特定于测试的设置和拆卸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要使用两个不同的设置创建单元测试,并使用两个不同的测试在同一个类中创建拆卸方法。
每个测试都将使用其在python单元测试框架中的特定设置和拆卸方法。
有人能帮帮我吗?
class processtestCase(unittest.TestCase):
print "start the process test cases "
def setUp1(self):
unittest.TestCase.setUp(self)
def test_test1(self):
"test Functinality"
def tearDown1(self):
unittest.TestCase.tearDown(self)
def setUp2(self):
unittest.TestCase.setUp2(self)
def test_test2(self):
"test Functinality"
def tearDown2(self):
unittest.TestCase.tearDown2(self) '
if __name__ == '__main__':
unittest.main()
推荐答案
在问题中,您提到有两个测试,每个测试都有自己的设置和拆除。至少有两条路可走:
您可以在每个测试中嵌入setUp
和tearDown
代码:
class FooTest(unittest.TestCase):
def test_0(self):
... # 1st setUp() code
try:
... # 1st test code
except:
... # 1st tearDown() code
raise
def test_1(self):
... # 2nd setUp() code
try:
... # 2nd test code
except:
... # 2nd tearDown() code
raise
或者,您也可以将类拆分为两个类:
class FooTest0(unittest.TestCase):
@classmethod
def setUp(cls):
...
@classmethod
def tearDown(cls):
...
def test(self):
...
第一个选项的类更少、更短、更直接。第二种方法更干净利落地去耦合设置装置,并清理它,然后是测试代码本身。它还在未来增加更多的测试。
您应该根据您的具体情况和个人喜好来判断取舍。
这篇关于如何在python unittest中指定特定于测试的设置和拆卸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在python unittest中指定特定于测试的设置和拆卸


猜你喜欢
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01