How to unify command line arguments?

I am trying to provide Python unittest command line unittest and run into some problems. I searched the web and found a way to provide arguments like

 unittest.main(argv=[myArg]) 

The problem is that this works great for a single command line argument, but not for more than one argument.

 unittest.main(argv=[myArg1, myArg2, myArg3]) 

The call above with the error below:

  File "/opt/python2.6.6/lib/python2.6/unittest.py", line 816, in __init__ self.parseArgs(argv) File "/opt/python2.6.6/lib/python2.6/unittest.py", line 843, in parseArgs self.createTests() File "/opt/python2.6.6/lib/python2.6/unittest.py", line 849, in createTests self.module) File "/opt/python2.6.6/lib/python2.6/unittest.py", line 613, in loadTestsFromNames suites = [self.loadTestsFromName(name, module) for name in names] File "/opt/python2.6.6/lib/python2.6/unittest.py", line 584, in loadTestsFromName parent, obj = obj, getattr(obj, part) AttributeError: 'module' object has no attribute 'admin' 

Dig even more and find out that Python unittest handles everything sent with argv as a test case to run.

Please let me know if there is another way to suggest more than one argument in my unit test cases. I want to override some hardcoded values, such as IP address, test tag tag, etc. And essentially run this script test from the main script test.

Thanks in advance.

+6
source share
3 answers

Why not just print the command line arguments before running unittest.main and then give it [sys.argv[0]] for your argv ?

Sort of:

 if __name__ == '__main__': # do stuff with sys.argv unittest.main(argv=[sys.argv[0]]) 

Note that when setting argv=None , unittest.main actually takes this as a signal for parsing sys.argv . unittest.main requires at least one argv element to be used as the program name. Therefore, avoiding None , [sys.argv[0]] is a good value, which gives as it considers that it has no command line arguments.


PS I just noticed your last sentence. If this is the case, do not use command line arguments. Your "main" test script should use the unittest API to load the test modules for the module, customizing them as you wish.

+10
source

Instead of actually sending the command from the command line, suppose OptionParser will complete its task and run the variables with input. If you have something like:

 from optparse import OptionParser parser = OptionParser() parser.add_option("-t", "--tag", dest="tag", help="tag id") 

Then try sowing the tag with the values ​​as if they came from the command line, and then pass them to the __init__ test classes.

+3
source

I have similar wishes in that I need a way to configure fake command line arguments for the test.

I found that overriding sys.argv inside every test I needed to work for me. The argument to the unittest.main() function that you describe is used for the unit test itself, and not for the module you want to test.

+1
source

Source: https://habr.com/ru/post/887711/


All Articles