4

I have a question about argparse. Here is part of my code:

(...)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-g', '--gfirst', dest="a", type=int, required=True)
    args = parser.parse_args()
    print args.a #Testing
    print args.a #Testing

if __name__ == '__main__':
    main()
    print "3"
    unittest.main(verbosity=2)
    print "4"

(...)

I am trying to set 'a' as a required value to execute the test cases, because I will need this value in the future. However...

$ python regular_test.py --gfirst 2
2
2
3
option --gfirst not recognized
Usage: regular-test.py [options] [test] [...]

Options:
  -h, --help       Show this message
  -v, --verbose    Verbose output
  -q, --quiet      Minimal output
  -f, --failfast   Stop on first failure
  -c, --catch      Catch control-C and display results
  -b, --buffer     Buffer stdout and stderr during test runs

...as you can see, the program accepts the argument and prints it, but the test case itself does not execute. I've inserted some prints to show whats executing and what isnt.

What am I doing wrong? Thanks in advance

ps.: I am using python 2.7.3
ps2.:The tests were running properly (before adding argparse to the program.)

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
almanegra
  • 653
  • 8
  • 21
  • What is `unittest.main`? It apparently defines a different argument parser than the one in `regular_test.main`. – chepner May 08 '14 at 21:01
  • https://docs.python.org/2/library/unittest.html#basic-example shows a basic example of calling unittest.main(). The skeleton of my code is like that – almanegra May 08 '14 at 21:03
  • 1
    Then somewhere in the portion of `regular_test.py` that you aren't showing is a test function which defines a different argument parser that doesn't recognize a `--gfirst` argument, or rather `unittest.main` as its own parser which also takes command line arguments, just not `--gfirst`. – chepner May 08 '14 at 21:06

2 Answers2

4

unittest.main() itself parses command-line arguments and it cannot understand/recognize your custom defined arguments (see parseArgs() method of the TestProgram class).

Instead, run tests using TextTestRunner:

runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(unittest.makeSuite(MyTestCase))

Also see related threads:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • That's probably the problem. I didn't google for unittest because I thought the problem was related to argparse. Thank you all. – almanegra May 08 '14 at 21:16
  • @Miller yeah, even the [docs](https://docs.python.org/2/library/unittest.html#unittest.main) are saying: `unittest.main() is a command-line program that loads a set of tests from module and runs them.` – alecxe May 08 '14 at 21:17
1

Your problem is that the unit-tester wants to own any command-line arguments. It might make it complicated to prescribe your own arguments.

Technically, unit-tests should contain everything they need in order to run, and shouldn't depend on arguments. You might consider moving any environment-related configuration (like a DB hostname, for example) to environment variables.

My two cents.

Dustin Oprea
  • 9,673
  • 13
  • 65
  • 105