How to unittest command line arguments?

2024/10/8 6:18:58

I am trying to supply command line arguments to Python unittest and facing some issues. I have searched on internet and found a way to supply arguments as

unittest.main(argv=[myArg])

The issue is this works fine for single command line argument but fails for more than one arguments.

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

Above call fails with below error:

  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 parseArgsself.createTests()File "/opt/python2.6.6/lib/python2.6/unittest.py", line 849, in createTestsself.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'

Digged more into this and found that Python unittest treats everything sent using argv as test case to be run.

Please let me know If there's still a way to supply more than one arguement to my unit test cases. I want to override some hard coded values like IP address, test case tag etc. and essentially run this test script from within main test script.

Thanks in advance.

Answer

Why not just take out the command line arguments before running unittest.main, and then give it [sys.argv[0]] for its argv?

Something like:

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

Note that when given argv=None, unittest.main actually takes this as a signal to parse sys.argv. unittest.main requires at least one argv element to use as the program name. So avoiding None, [sys.argv[0]] is a good value to give since then it thinks it has no command-line arguments.


P.S. I just noticed your last sentence. If that's the case - don't use command-line arguments. Your "main" test script should just use unittest's APIs to load testcases for module, customizing them at its wish.

https://en.xdnf.cn/q/70155.html

Related Q&A

different foreground colors for each line in wxPython wxTextCtrl

I have a multilinewx.TextCtrl()object which I set its forground and Background colors for writing strings.I need to write different lines with different colors ,wx.TextCtrl.setForgroundcolor()changes a…

Access deprecated attribute validation_data in tf.keras.callbacks.Callback

I decided to switch from keras to tf.keras (as recommended here). Therefore I installed tf.__version__=2.0.0 and tf.keras.__version__=2.2.4-tf. In an older version of my code (using some older Tensorfl…

How to unpickle a file that has been hosted in a web URL in python

The normal way to pickle and unpickle an object is as follows:Pickle an object:import cloudpickle as cpcp.dump(objects, open("picklefile.pkl", wb))UnPickle an object: (load the pickled file):…

Control tick-labels from multi-level FactorRange

Ive got a three-level bokeh.models.FactorRange which I use to draw tick labels on a vbar-plot. The problem is that there are dozens of factors in total and the lowest-level labels get very cramped.I ca…

PyTorch torch_sparse installation without CUDA

I am new in PyTorch and I have faced one issue, namely I cannot get my torch_sparse module properly installed. In general, I wanted to use module torch_geometric - this I have installed. However, when …

Escaping XPath literal with Python

Im writing a common library to setup an automation test suite with Selenium 2.0 Pythons webdriver.def verify_error_message_present(self, message):try:self.driver.find_element_by_xpath("//span[@cla…

How to return two values in cython cdef without gil (nogil)

I have a function and I am trying to return a number and a vector of ints. What I have is cdef func() nogil:cdef vector[int] vectcdef int a_number...return a_number, vectbut this will give errors like …

Alias for a chain of commands

I have a tool with commands: step1, step2 and step3.I can chain them by calling:$ tool step1 step2 step3I would like to have an alias named all to run all the steps by calling:$ tool allI have found a …

Generate misspelled words (typos)

I have implemented a fuzzy matching algorithm and I would like to evaluate its recall using some sample queries with test data. Lets say I have a document containing the text:{"text": "T…

Get the inverse function of a polyfit in numpy

I have fit a second order polynomial to a number of x/y points in the following way:poly = np.polyfit(x, y, 2)How can I invert this function in python, to get the two x-values corresponding to a speci…