为了账号安全,请及时绑定邮箱和手机立即绑定

如何避免在测试用例失败后执行 Python UnitTest 方法“addCleanup”?

如何避免在测试用例失败后执行 Python UnitTest 方法“addCleanup”?

交互式爱情 2021-10-19 16:06:39
我有以下 Python UnitTest:import unittestclass SimpleTestCases(unittest.TestCase):    def setUp(self):        print "\nmessage from function: setUp"        self.createResource()        self.addCleanup(self.cleanResource)    def createResource(self):        print "\nmessage from function: createResource"    def cleanResource(self):        print "\nmessage from function: cleanResource"    def test_func1(self):        print "message from function: test_func1::start"        print "message from function: test_func1::end"    def test_func2(self):        print "message from function: test_func2::start"        self.assertTrue(False)        print "message from function: test_func2::end"    def test_func3(self):        print "message from function: test_func3::start"        print "message from function: test_func3:end"if __name__ == "__main__":    unittest.main()上面带有选项“-f”的代码执行后,函数按以下顺序执行python -m unittest -fb test_casesmessage from function: setUpmessage from function: createResourcemessage from function: test_func1::startmessage from function: test_func1::endmessage from function: cleanResource.message from function: setUpmessage from function: createResourcemessage from function: test_func2::startFmessage from function: cleanResource======================================================================FAIL: test_func2 (test_cases.SimpleTestCases)----------------------------------------------------------------------Traceback (most recent call last):  File "test_cases.py", line 23, in test_func2    self.assertTrue(False)AssertionError: False is not true----------------------------------------------------------------------Ran 2 tests in 0.000sFAILED (failures=1)它会在第一次失败后停止执行,但会调用“addCleanup”方法。我想避免在测试函数“test_func2”时调用“addCleanup”方法,以便创建的资源可用于调试目的。是否有可用的命令行选项或解决方法?
查看完整描述

2 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

作为最简单的方法之一 - 只是将输入(Python 2 的 raw_input)放在 cleanResource 方法的开头。所以执行将停止,直到按下 Enter 键。


def cleanResource(self):

    temp = raw_input('wait before cleanResource')

    print("\nmessage from function: cleanResource")

另一种方法是在调试器中使用断点


import pdb

进而


pdb.set_trace()

需要的地方


查看完整回答
反对 回复 2021-10-19
?
浮云间

TA贡献1829条经验 获得超4个赞

根据单元测试文档doCleanups()是负责调用所有清理方法的方法。


一种方法是在 doCleanups 方法中检查测试用例的结果,如果测试用例失败则弹出所有清理方法并跳过其余测试用例


下面是代码:


import unittest


class SimpleTestCases(unittest.TestCase):

    FAILURE = False


    def setUp(self):

        print "\nmessage from function: setUp"

        if SimpleTestCases.FAILURE:

            self.skipTest("Test is skipped due to first failure")


            return super(SimpleTestCases, self).setUp()


        self.createResource()

        self.addCleanup(self.cleanResource)


    def createResource(self):

        msg = "\nmessage from function: createResource"


    def cleanResource(self):

        print "\nmessage from function: cleanResource"


    def test_func1(self):

        print "message from function: test_func1::start"

        print "message from function: test_func1::end"


    def test_func2(self):

        print "message from function: test_func2::start"


        self.assertTrue(False)


        print "message from function: test_func2::end"


    def test_func3(self):

        print "message from function: test_func3::start"

        print "message from function: test_func3:end"


    def doCleanups(self):

        print "message from function: doCleanups"

        if SimpleTestCases.FAILURE:

            return super(SimpleTestCases, self).doCleanups()


        result = getattr(self, '_outcomeForDoCleanups', self._resultForDoCleanups)


        ok_result = True

        exc_list = getattr(result, 'failures')


        if exc_list and exc_list[-1][0] is self:

            ok_result = ok_result and not exc_list[-1][1]


        if not ok_result:

            SimpleTestCases.FAILURE = True

            while self._cleanups:

                (func, args, kwargs) = self._cleanups.pop()


        return super(SimpleTestCases, self).doCleanups()


if __name__ == "__main__":

    result = unittest.main()

执行为


python -m unittest test_cases

和输出:


message from function: setUp

message from function: test_func1::start

message from function: test_func1::end

message from function: doCleanups


message from function: cleanResource

.

message from function: setUp

message from function: test_func2::start

Fmessage from function: doCleanups


message from function: setUp

smessage from function: doCleanups


======================================================================

FAIL: test_func2 (test_cases.SimpleTestCases)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "test_cases.py", line 30, in test_func2

    self.assertTrue(False)

AssertionError: False is not true


----------------------------------------------------------------------

Ran 3 tests in 0.000s


FAILED (failures=1, skipped=1)


查看完整回答
反对 回复 2021-10-19
  • 2 回答
  • 0 关注
  • 285 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信