gpt4 book ai didi

python - python测试套件执行完毕后执行函数

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:25 25 4
gpt4 key购买 nike

我正在使用 python unittest 框架来做一些测试。

class AbstractTest(unittest.TestCase):
def setUp(self):


def tearDown(self):
# Close!
self.transport.close()

def testVoid(self):
self.client.testVoid()

def testString(self):
global test_basetypes_fails
try:
self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20)
except AssertionError, e:
test_basetypes_fails = True
print test_basetypes_fails
raise AssertionError( e.args )
try:
self.assertEqual(self.client.testString(''), '')
except AssertionError, e:
test_basetypes_fails = True
raise AssertionError( e.args )

def testByte(self):
global test_basetypes_fails
try:
self.assertEqual(self.client.testByte(63), 63)
except AssertionError, e:
test_basetypes_fails = True
raise AssertionError( e.args )
try:
self.assertEqual(self.client.testByte(-127), -127)
except AssertionError, e:
test_basetypes_fails = True
raise AssertionError( e.args )

@classmethod
def tearDownClass(cls):
#sys.exit(1)

当我执行测试时,我得到以下结果。

..................
----------------------------------------------------------------------
Ran 18 tests in 2.715s

OK

我需要在执行完后执行一段程序。我怎样才能做到这一点?当我将代码添加到类级别时,它会在完成以下部分输出后执行它。

..................

最佳答案

您需要编写自己的测试运行器,以便根据套件的结果返回退出代码。

您需要做的一切都在 unittest-module 文档中进行了解释。使用 TestLoader 加载您的套件,并使用 TextTestRunner 运行它。然后根据套件的结果,使用适当的退出代码调用 sys.exit。

关于python - python测试套件执行完毕后执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25402293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com