[issue39283] Add ability to inherit unittest arguement parser

Ajay Tripathi report at bugs.python.org
Tue Jan 14 02:01:28 EST 2020


Ajay Tripathi <ajay39in at gmail.com> added the comment:

Hi,

Thanks for your response, here is a code sample of what I am doing right now:

```
import unittest
import sys

class MyTest(unittest.TestCase):

    def __init__(self, testName, extraArg):
        super(MyTest, self).__init__(testName)
        self.myExtraArg = extraArg

    def test_something(self):
        print(self.myExtraArg)

extraArg = sys.argv.pop()
suite = unittest.TestSuite()
suite.addTest(MyTest('test_something', extraArg))
unittest.TextTestRunner(verbosity=2).run(suite)
```

It works.
However, this can be cumbersome if there are a lot of "extraArgs" and MyTestClasses.
Especically, if there is an existing argparser function existing that is used in the program, re-writing the code for passing args in another way for unittest doesn't seem ideal.


Ideally, I think something like this may work perfectly:

```
import unittest


class SomethingLikeInheritedUnitest(unittest.TestProgram):
    # parent_parser is used here: https://github.com/python/cpython/blob/master/Lib/unittest/main.py#L162
    parent = super().createParentArgParserAndReturnIt()
    parser = argparse.ArgumentParser(parents=[parent])
    parser.add_argument('--myarg1', ...)
    parser.add_argument('--myarg2', ...)
    return parser
```

Now the parser may be exposed to user's MyTest class and they can use
arguements as: `paser.myarg1`


Note: I am not certain this may be the way to go about it, there may be a better way, infact, I've not even had the time to read the entire code. :-( 
I am only trying to point out is that the ability to add `myarg1` in the parent_parser may be useful at times.

Hope this makes the point a bit more clear. If I have misunderstood something please correct me.



Thank You,
Ajay Tripathi

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39283>
_______________________________________


More information about the Python-bugs-list mailing list