How to select some test methods in a fixture using

2019-08-12 08:58发布

Below is a pared down illustrative representation of my problem :

import sys
import nose


def test_equality_stdalone():
    assert "b" == "b"


def test_inequality_stdalone():
    assert "b" != "c"


def test_xxx_stdalone():
    assert "xxx" == "xxx"


class TestClass(object):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_equality(self):
        assert "a" == "a"

    def test_xxx(self):
        assert "xxx" == "xxx"


if __name__ == "__main__":
    nose.main(argv=sys.argv[:] + ["-v", "-m", ".*equality.*", __file__])

This test script upon execution yields -

zzztest.test_equality_stdalone ... ok
zzztest.test_inequality_stdalone ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK

While this -

nose.main(argv=sys.argv[:] + ["-v", "-m", ".*Class.*", __file__])

yields -

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

And this -

nose.main(argv=sys.argv[:] + ["-v", "-m", "", __file__])

yields -

zzztest.TestClass.test_equality ... ok
zzztest.TestClass.test_xxx ... ok
zzztest.test_equality_stdalone ... ok
zzztest.test_inequality_stdalone ... ok
zzztest.test_xxx_stdalone ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.004s

OK

Could someone please help me in figuring out how to select only a few methods from the TestClass for testing?

P.S. - As per documentation (below) nose.main(argv=sys.argv[:] + ["-v", "-m", ".*Class.*", __file__]) should have picked up at least a couple of tests.

  -m REGEX, --match=REGEX, --testmatch=REGEX
                        Files, directories, function names, and class names
                        that match this regular expression are considered
                        tests.  Default: (?:^|[\b_\.\-])[Tt]est
                        [NOSE_TESTMATCH]

P.P.S. - Not a duplicate of nose framework command line regex pattern matching doesnt work(-e,-m ,-i). If you read the question and saw the 3 different inputs that I pass to nose.main() then you would see that the problem is not that the "-m" filter is not working at all, but that it works only for standalone test cases while always ignoring methods from TestClass.

1条回答
虎瘦雄心在
2楼-- · 2019-08-12 09:45

It is important to understand how nose does matching for what it wants as test. It happens independently for class names, function names and directories. Have a look at selector.py to clear up the mystery, but the short story is: your regex query must match the class name (TestClass in your case) AND class methods (test_equality or test_xxx) simultaneously. So if you would like to run TestClass.test_xxx you use something like:

nosetests zzztest.py -v --match="(TestClass|test_xxx$)"
zzztest.TestClass.test_xxx ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

If the class is not matched as part of the regex, it's methods are not viewed as tests and will not be evaluated against regex at all, thus you are getting 0 tests.

The only differentiator here is the dollar sign that matches class test method, but fails to match standalone method. If you have standalone and class methods named the same, you will not be able to differentiate between the two using --match regex filter.

查看更多
登录 后发表回答