scphantm
scphantm

Reputation: 4563

create a private class in a test method

I am trying to do this in a test method:

def test_init(self):
    class args:
        def __init__(self):
            self.server = "myserver"
            self.project = "myproject"

    print(args.server)
    print(args.project)
    CommandFile(args)

on first appearance, it looks like it should work just fine, passing an args object to my class constructor. but for some reason its not. Pretty sure its a syntax issue, but I never tried this with python before so im not sure. any ideas?

Upvotes: 0

Views: 24

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599796

You're confused between class and instance variables. args is a class, and doesn't have server or project attributes. An instance of args would have those attributes, because they're assigned in __init__, but you never instantiate an object so that method is never called.

If you stuck to standard PEP8 naming syntax the confusion would be less likely to arise, since you would call the class Args and an instance args.

Upvotes: 2

Related Questions