nose icon indicating copy to clipboard operation
nose copied to clipboard

skipUnless applied to a class doesn't skip setUpClass

Open nedbat opened this issue 8 years ago • 8 comments

I applied skipUnless to a class. The condition is false, so the class' test methods are not run. But the class' setUpClass method still is. Unittest and py.test both skip the setUpClass method.

test_skipper.py:

import unittest


class MyTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        super(MyTest, cls).setUpClass()
        print "MyTest.setUpClass"

    def test_one(self):
        print "MyTest.test_one"

    def test_two(self):
        print "MyTest.test_two"


@unittest.skipUnless(False, "NEVER RUN THIS")
class MySkippedTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        super(MySkippedTest, cls).setUpClass()
        print "MySkippedTest.setUpClass"

    def test_one(self):
        print "MySkippedTest.test_one"

    def test_two(self):
        print "MySkippedTest.test_two"

Test runs with nose, py.test, and unittest:

$ nosetests -s
MySkippedTest.setUpClass
SSMyTest.setUpClass
MyTest.test_one
.MyTest.test_two
.
----------------------------------------------------------------------
Ran 4 tests in 0.004s

OK (SKIP=2)

$ py.test -s
===================================================================== test session starts ======================================================================
platform darwin -- Python 2.7.8 -- py-1.4.30 -- pytest-2.7.2
rootdir: /Users/ned/foo/skipper, inifile:
collected 4 items

test_skipper.py MyTest.setUpClass
MyTest.test_one
.MyTest.test_two
.ss

============================================================= 2 passed, 2 skipped in 0.02 seconds ==============================================================

$ python -m unittest discover .
ssMyTest.setUpClass
MyTest.test_one
.MyTest.test_two
.
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK (skipped=2)

nedbat avatar Aug 27 '15 15:08 nedbat

All versions of skip seems to fail:

import unittest

from hamcrest import assert_that, equal_to

@unittest.skip("Don't need it")
class TestSkip(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        assert False, "Should never be called"

    def test_skip(self):
        self.fail("Test wasn't meant to run")


@unittest.skipIf(True, "Don't need it")
class TestSkipIf(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        assert False, "Should never be called"

    def test_skip(self):
        self.fail("Test wasn't meant to run")



@unittest.skipUnless(False, "Don't need it")
class TestSkipUnless(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        assert False, "Should never be called"

    def test_skip(self):
        self.fail("Test wasn't meant to run")

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

All three tests fail with "Should never be called"

stefan-imp avatar Sep 24 '15 15:09 stefan-imp

I saw the same issue too. Environment: Debian 8 + Python 3.4 + nosetests 1.3.7.

stanleymeng avatar Sep 29 '15 18:09 stanleymeng

Guys, the issue here is that a class is being marked as skip and our test runner knows nothing about it--the current form of the skip plugin relies on a Skip exception being raised, and that doesn't occur with classes that have been marked for skipping.

It requires adding new plugin hooks to solve the problem, and I'm a bit loathe to do that since Nose2 has been deemed the way forward. :-(

jszakmeister avatar Sep 29 '15 18:09 jszakmeister

I'm having this issue on Python 2.7 + Centos6.5 + nosetests 1.3.7 as well as Python 2.7 + OSX10.10 + nosetests 1.3.7 My tests are written using standard python unit test framework but I use the nose tests runner for the xUnit style reports (and also so we can write tests in either nosetests or unittests)

jessechahal avatar Aug 17 '16 17:08 jessechahal

We fixed this issue in small plugin: https://github.com/2gis/lode_runner/pull/31

z00sts avatar Feb 15 '18 04:02 z00sts

how do you use that @z00sts ?

frankandrobot avatar Jan 03 '19 20:01 frankandrobot

@frankandrobot You should use nose runner wrapper lode_runner instead of raw nose Check out docs and feel free to ask questions on lode_runner project page.

z00sts avatar Jan 15 '19 03:01 z00sts

an alternative is to raise SkipTest at the start of the setUpClass function like the following:

class SomeTestClass(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        if some_condition:
            raise unittest.SkipTest(some_message)
        # start using setUpClass as you would normally

stsoor avatar Aug 10 '19 07:08 stsoor