4

If you call the program below with python filename.py -d abc you get unittest help. If you call filename.py /d abc you get:

AttributeError: 'module' object has no attribute '/d'

I'd like to create my own CLI switches. To specify config file or some such CLI switch. I've tried --d as well. Is there a method for unittest to accept allow other switches?

import unittest

class SomeTests(unittest.TestCase):
    def test_one(self):
        theTest( 'keith' )

    def test_two(self):
        otherTest( 'keith')

if __name__ == '__main__':
    unittest.main( argv=sys.argv, testRunner = unittest.TextTestRunner(verbosity=2))
Keith
  • 4,129
  • 3
  • 13
  • 25

1 Answers1

0

Use sys and getopt modules to parse command-line options and arguments, allowing you to use short and long options (e.g. -x and --long-option).

mtm
  • 691
  • 6
  • 6
  • How is this mixed with unittest since unittest still seams to balk at any additional flags. – Keith May 08 '19 at 17:33
  • Add code to **parse args** with `for` loop and for each arg you will do something (e.g. set a variable or run some code). Check out [How to use getopt/OPTARG in Python?](https://stackoverflow.com/questions/6382804/how-to-use-getopt-optarg-in-python-how-to-shift-arguments-if-too-many-arguments) – mtm May 08 '19 at 17:56
  • Figured it out. You call parse_known_args() without any arguments. It passes back two lists. parsed args and unparsed args (or additional args). Unfortunately unit test removed arg[0] so I just put it back. This is exactly what I needed to make unit test happy. – Keith May 11 '19 at 00:17