jeudi 31 mars 2016

Python Unit test: Assertion Error issue

I am writing a unit test for the following function:

def _parse_args():
    parser = argparse.ArgumentParser(
        description='Script to configure appliance.'
    )
    parser.add_argument('--config_file',
                        help='Path to a JSON configuration file') 
    print parser.parse_args()
    return parser.parse_args()

When I run the function when no config file is given then (verified using the print statement in the function above): parser.parse_args()=Namespace(config_file=None)

In my unit test I run the function with no config file given and include an assertEquals:

self.assertEquals(my_script._parse_args(), 'Namespace(config_file=None)')

But this produces the AssertionError:

AssertionError: Namespace(config_file=None) != 'Namespace(config_file=None)'

If I change the unit test to without the quotation marks:

self.assertEquals(my_script._parse_args(), Namespace(config_file=None))

I get a NameError:

NameError: global name 'Namespace' is not defined

Clearly using quotations is not the correct way to do this but how do I get it to assert that Namespace(config_file=None) is occurring?

Aucun commentaire:

Enregistrer un commentaire