Performing Generated Nasal Tests

Suppose I define the testFile.py python module as follows.

 def test_evens(): for i in range(0, 5): yield check_even, i, i*3 def check_even(n, nn): assert n % 2 == 0 or nn % 2 == 0 

When I let the nose identify the tests in collection-only mode, I get

 testFile.test_evens(0, 0) ... ok testFile.test_evens(1, 3) ... ok testFile.test_evens(2, 6) ... ok testFile.test_evens(3, 9) ... ok testFile.test_evens(4, 12) ... ok 

I can run all tests using

nosetests -v testFile: test_evens

However, what if I only want to run testFile.test_evens (2, 6) (i.e. not all tests)?

Is there any way to do this from the command line?

+6
source share
1 answer

The nose cannot do this by default, as far as I know. Here are a few options:

1. Fake it from the command line

Probably not what you are looking for, but I should have mentioned it. You can also create a wrapper script to simplify this:

 python -c 'import testFile; testFile.check_even(2, 6)' 

2. Create your own nose test loader

This is a little more, but you can create your own test loader, which interprets the command line arguments by asking the generator to load, pull the tests and arguments from the generator, and return a set containing the test with the corresponding arguments.

Below is an example of code that should give you enough room to create ( runner.py ):

 import ast import nose class CustomLoader(nose.loader.TestLoader): def loadTestsFromName(self, name, module=None): # parse the command line arg parts = name.split('(', 1) mod_name, func_name = parts[0].split('.') args = ast.literal_eval('(' + parts[1]) # resolve the module and function - you'll probably want to # replace this with nose internal discovery methods. mod = __import__(mod_name) func = getattr(mod, func_name) # call the generator and gather all matching tests tests = [] if nose.util.isgenerator(func): for test in func(): _func, _args = self.parseGeneratedTest(test) if _args == args: tests.append(nose.case.FunctionTestCase(_func, arg=_args)) return self.suiteClass(tests) nose.main(testLoader=CustomLoader) 

Performance:

 % python runner.py 'testFile.test_evens(2, 6)' -v testFile.check_even(2, 6) ... ok % python runner.py 'testFile.test_evens(2, 6)' 'testFile.test_evens(4, 12)' -v testFile.check_even(2, 6) ... ok testFile.check_even(4, 12) ... ok % python runner.py 'testFile.test_evens(1, 3)' -v testFile.check_even(1, 3) ... FAIL 
+7
source

Source: https://habr.com/ru/post/885447/


All Articles