nose framework command line regex pattern matching

2019-04-07 20:05发布

问题:

The python nosetest framework has some command line options to include, exclude and match regex for tests which can be included/excluded and matched respectively.

However they don't seem to be working correctly.

[kiran@my_redhat test]$ nosetests -w cases/ -s -v  -m='_size'
----------------------------------------------------------------------
Ran 0 tests in 0.001s
OK
[kiran@my_redhat test]$ grep '_size' cases/test_case_4.py
    def test_fn_size_sha(self):

is there some thing wrong with regex matching semantics of nose framework?

回答1:

Nosetests' -m argument is used to match directories, filenames, classes, and functions. (See the nose docs explanation of this parameter) In your case, the filename of your test file (test_case_4.py) does not match the -m match expression (_size), so is never opened.

You may notice that if you force nose to open your test file, it will run only the specified test:

nosetests -sv -m='_size' cases/test_case_4.py

In general, when I want to match specific tests or subsets of tests I use the --attrib plugin, which is available in the default nose install. You may also want to try excluding tests that match some pattern.



回答2:

Try removing '=' when specifying the regexp:

$ nosetests -w cases/ -s -v -m '_size'

or keep '=' and spell out --match:

$ nosetests -w cases/ -s -v --match='_size'


回答3:

Nose is likely using Python's re.match, or something equivalent, which requires a match at the beginning of the string. _size doesn't match because the function name test_fn_size_sha doesn't start with the regex _size.

Try using a regex that matches from the beginning:

nosetests -w cases/ -s -v -m='\w+_size'