Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot run individual tests with nosetests #17

Closed
omaciel opened this issue Feb 21, 2014 · 4 comments
Closed

Cannot run individual tests with nosetests #17

omaciel opened this issue Feb 21, 2014 · 4 comments

Comments

@omaciel
Copy link

omaciel commented Feb 21, 2014

Seems that you cannot run an individual ddt test via nosetests:

$ cat test_foo.py
import unittest
from ddt import ddt, data


@ddt
class FooTestCase(unittest.TestCase):

    @data(3, 4, 12, 23)
    def test_larger_than_two(self, value):
            self.assertTrue(value > 0)

$ nosetests -vv test_foo.py:FooTestCase
nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
test_larger_than_two_12 (tests.cli.test_foo.FooTestCase) ... ok
test_larger_than_two_23 (tests.cli.test_foo.FooTestCase) ... ok
test_larger_than_two_3 (tests.cli.test_foo.FooTestCase) ... ok
test_larger_than_two_4 (tests.cli.test_foo.FooTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK

$ nosetests -vv test_foo.py:FooTestCase.test_larger_than_two
nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
Failure: ValueError (No such test FooTestCase.test_larger_than_two) ... ERROR

======================================================================
ERROR: Failure: ValueError (No such test FooTestCase.test_larger_than_two)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/nose/failure.py", line 41, in runTest
    raise self.exc_class(self.exc_val)
ValueError: No such test FooTestCase.test_larger_than_two

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
@txels
Copy link
Collaborator

txels commented Feb 21, 2014

DDT creates four tests out of one, as you can see in output these are named:

test_larger_than_two_12
test_larger_than_two_23
test_larger_than_two_3
test_larger_than_two_4

So test_larger_than_two is no longer an individual test (that was kind of the whole point of ddt).
From what I've gathered, there is no way to issue a nosetests command-line that would run these four tests (e.g. by matching function name with a regex).

That said, you should be able to run them individually:

nosetests -vv test_foo.py:FooTestCase.test_larger_than_two_4

But nose still fails with:

$ nosetests -v test/test_example.py:FooTestCase.test_larger_than_two_4
Failure: ValueError (no such test method in <class 'test.test_example.FooTestCase'>: test_larger_than_two) ... ERROR

======================================================================
ERROR: Failure: ValueError (no such test method in <class 'test.test_example.FooTestCase'>: test_larger_than_two)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/carles/.virtualenvs/ddt/local/lib/python2.7/site-packages/nose/loader.py", line 518, in makeTest
    return self._makeTest(obj, parent)
  File "/home/carles/.virtualenvs/ddt/local/lib/python2.7/site-packages/nose/loader.py", line 572, in _makeTest
    return parent(obj.__name__)
  File "/usr/lib/python2.7/unittest/case.py", line 191, in __init__
    (self.__class__, methodName))
ValueError: no such test method in <class 'test.test_example.FooTestCase'>: test_larger_than_two

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

So it may be something related with how nose loads/runs the tests when running an individual method. I'll try to debug and see if something can be done within ddt.

@omaciel
Copy link
Author

omaciel commented Feb 21, 2014

Thank you @txels for the explanation (totally makes sense to me). Since I usually generate random data for my tests, it would be impossible to know ahead of time what the test name would be. I really appreciate anything you can do to help :)

@txels
Copy link
Collaborator

txels commented Feb 21, 2014

I've been able to fix this. Turns out there was a mismatch between the new function as attribute in the test class and the function name of said function. Released as 0.7.1

Now I can do:

$ nosetests -s -v test/test_example.py:FooTestCase.test_larger_than_two_3
test_larger_than_two_3 (test.test_example.FooTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Or even:

$ nosetests -s -v 'test/test_example.py:FooTestCase.test_tuples_extracted_into_arguments_(3, 2)'
test_tuples_extracted_into_arguments_(3, 2) (test.test_example.FooTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

But unfortunately nosetests doesn't allow me to select all functions matching a name... (something like FooTestCase.test_larger_than_two*). Will have to submit a patch to nose for that ;)

@txels txels closed this as completed Feb 21, 2014
@omaciel
Copy link
Author

omaciel commented Feb 21, 2014

Though this won't help me with my scenario (where @DaTa uses random values every time), I'm glad you fixed the other use case :) Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants