如果有setUp函数,untest.skip就不工作了。

问题描述 投票:0回答:1

在下面的代码中

class TestSomething(unittest.TestCase):
    def setUp(self):
        print("setting up")

    @unittest.skip("skip reason")
    def test_1(self):
        print("in test 1")

    def test_2(self):
        print("in test 2")

我只期望 test_2 来运行。但两个测试都在运行。我怀疑这是由于 setUp 函数,因为如果我删除它,那么只有 test_2 运行,如期而至。

有什么解决方法吗?

python-3.x unit-testing python-unittest
1个回答
0
投票

@unittest.skip(reason) 在以下情况下工作正常 setUp 方法,无法重现。

例如test_something.py:

import unittest


class TestSomething(unittest.TestCase):
    def setUp(self):
        print("setting up")

    @unittest.skip("skip reason")
    def test_1(self):
        print("in test 1")

    def test_2(self):
        print("in test 2")


if __name__ == '__main__':
    unittest.main()

单元测试结果。

ssetting up
in test 2
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK (skipped=1)
© www.soinside.com 2019 - 2024. All rights reserved.