Skip to content

When I use generator tests, setUp() doesn't get run before the test #80

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

Open
fasaxc opened this issue May 8, 2013 · 12 comments
Open

When I use generator tests, setUp() doesn't get run before the test #80

fasaxc opened this issue May 8, 2013 · 12 comments

Comments

@fasaxc
Copy link

fasaxc commented May 8, 2013

I wrote a test like this:

def test_rcv_request(self):
    def do_rcv_fanout_test(type, function_name):
        ...
    yield do_rcv_fanout_test, "create", "create_it"
    yield do_rcv_fanout_test, "create", "create_it"

The tests are detected and run but self.setUp() doesn't get run before either test.

@jpellerin
Copy link
Member

I can't reproduce this. With this test module:

from nose2.compat import unittest

class Test(unittest.TestCase):
    def setUp(self):
        print "setup!"

    def test_rcv_request(self):
        def do_rcv_fanout_test(type, function_name):
            print type, function_name
        yield do_rcv_fanout_test, "create", "create_it"
        yield do_rcv_fanout_test, "create", "create_it"

I get this output:

$ nose2 -v
test_rcv_request:1
'create', 'create_it' (test.Test) ... setup!
create create_it
ok
test_rcv_request:2
'create', 'create_it' (test.Test) ... setup!
create create_it
ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

@fasaxc
Copy link
Author

fasaxc commented May 9, 2013

Ah, looks like setup is being run but on the wrong instance:

class Test(unittest.TestCase):
    def setUp(self):
        print "setup! instance: ", id(self)
        self.foo = "Bar"

    def test_rcv_request(self):
        def do_rcv_fanout_test(type, function_name):
            print "Test instance: ", self, id(self)
            print type, function_name, self.foo
        yield do_rcv_fanout_test, "create", "create_it"
        yield do_rcv_fanout_test, "create", "create_it"

Gives

setup! instance: 3073307340
Test instance: test_rcv_request (src.meta.switchmgr.test.testswitchmgr.Test) 3073306764
Ecreate create_it setup! instance: 3071367884
Test instance: test_rcv_request (src.meta.switchmgr.test.testswitchmgr.Test) 3073306764
E

Note the different ID numbers between the setUp and test being run.

@jpellerin
Copy link
Member

That's correct behavior, actually. The generator tests are generated as
real test cases, so just like normal test case methods each one is run in a
new instance of the test case class.

On Thu, May 9, 2013 at 12:15 PM, Shaun Crampton [email protected]:

Ah, looks like setup is being run but on the wrong instance:

class Test(unittest.TestCase):
def setUp(self):
print "setup! instance: ", id(self)
self.foo = "Bar"

def test_rcv_request(self):
    def do_rcv_fanout_test(type, function_name):
        print "Test instance: ", self, id(self)
        print type, function_name, self.foo
    yield do_rcv_fanout_test, "create", "create_it"
    yield do_rcv_fanout_test, "create", "create_it"

Gives

setup! instance: 3073307340
Test instance: test_rcv_request
(src.meta.switchmgr.test.testswitchmgr.Test) 3073306764
Ecreate create_it setup! instance: 3071367884
Test instance: test_rcv_request
(src.meta.switchmgr.test.testswitchmgr.Test) 3073306764
E

Note the different ID numbers between the setUp and test being run.


Reply to this email directly or view it on GitHubhttps://github.com//issues/80#issuecomment-17673329
.

@fasaxc
Copy link
Author

fasaxc commented May 9, 2013

But shouldn't the setUp also be run on the same generated test case? I was expecting a generated test to behave just as if I'd added multiple extra test methods to my class. Right now, the code above hits an exception when it tries to access self.foo because self points to a different instance to the setUp.

@jpellerin
Copy link
Member

Ah, I see what you're saying now. Sorry I misunderstood. That does seem
like a bug.

On Thu, May 9, 2013 at 12:49 PM, Shaun Crampton [email protected]:

But shouldn't the setUp also be run on the same generated test case? I was
expecting a generated test to behave just as if I'd added multiple extra
test methods to my class. Right now, the code above hits an exception when
it tries to access self.foo because self points to a different instance to
the setUp.


Reply to this email directly or view it on GitHubhttps://github.com//issues/80#issuecomment-17675198
.

@jpellerin
Copy link
Member

The parameterize plugin looks like it behaves correctly, so the fix is
probably to make the generator plugin work more like parameterize. Which
would be a good thing anyway.

On Thu, May 9, 2013 at 12:55 PM, jason pellerin [email protected] wrote:

Ah, I see what you're saying now. Sorry I misunderstood. That does seem
like a bug.

On Thu, May 9, 2013 at 12:49 PM, Shaun Crampton [email protected]:

But shouldn't the setUp also be run on the same generated test case? I
was expecting a generated test to behave just as if I'd added multiple
extra test methods to my class. Right now, the code above hits an exception
when it tries to access self.foo because self points to a different
instance to the setUp.


Reply to this email directly or view it on GitHubhttps://github.com//issues/80#issuecomment-17675198
.

@fasaxc
Copy link
Author

fasaxc commented May 9, 2013

Ah, the parametrize plugin looks like it'll be nicer for my use case anyway. I'll switch to that.

@jpellerin
Copy link
Member

Parameterize is better for almost all cases, I think. But I should still
fix generators. :)

On Thu, May 9, 2013 at 2:48 PM, Shaun Crampton [email protected]:

Ah, the parametrize plugin looks like it'll be nicer for my use case
anyway. I'll switch to that.


Reply to this email directly or view it on GitHubhttps://github.com//issues/80#issuecomment-17682164
.

@trilogysci
Copy link

I'm also having this issue, but cannot use parameters because they are randomized values. Is there a quick fix?

@jpellerin
Copy link
Member

Maybe:

@params(*random_value_generator())
def test ...

... as far as fixing in nose2 itself, no, it won't be quick. The generators
plugin needs a pretty major rewrite.

On Wed, Jun 5, 2013 at 2:03 PM, trilogysci [email protected] wrote:

I'm also having this issue, but cannot use parameters because they are
randomized values. Is there a quick fix?


Reply to this email directly or view it on GitHubhttps://github.com//issues/80#issuecomment-18995662
.

@lucagiove
Copy link

This bug make impossibile to run tests on a data loaded for an external file or db

@lucagiove
Copy link

I made the fixture working by adding the variables in the class directly instead of creating them in setUpClass method

eg:

class TestAvro(unittest.TestCase):

    products = json.load(open('products.json'))
    contents = json.load(open('contents.json'))

    @classmethod
    def setUpClass(cls):
        pass

    def test_with_yield(self):

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

No branches or pull requests

4 participants