Peeyush Kushwaha
Peeyush Kushwaha

Reputation: 3623

unittest.skip not working if there is a setUp function

In the following code

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")

I expect only test_2 to run. But both the tests are running. I suspect that this is due to the setUp function, because if I remove it then only test_2 runs, as expected.

Is there a fix for this?

Upvotes: 1

Views: 516

Answers (1)

Lin Du
Lin Du

Reputation: 102207

@unittest.skip(reason) works fine with setUp method, can't reproduce it.

E.g. 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()

unit test results:

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

OK (skipped=1)

Upvotes: 1

Related Questions