I have the following sample code running unit tests:
#!/usr/bin/env python3
import sys
import unittest
import argparse
class ParentTest(unittest.TestCase):
None
class Test1(ParentTest):
def test_if_verbose(self):
import __main__ # FIXME
print("Success!") if __main__.args.verbose else "" # FIXME
class Test2(ParentTest):
None
if __name__ == '__main__':
# Parse arguments.
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-?", "--help", action="help", help="show this help message and exit" )
parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", help="increase output verbosity" )
parser.add_argument('files', nargs='*')
args = parser.parse_args()
print(args.verbose)
# Add tests.
alltests = unittest.TestSuite()
alltests.addTest(unittest.makeSuite(Test1))
alltests.addTest(unittest.makeSuite(Test2))
result = unittest.TextTestRunner(verbosity=2).run(alltests) # Run tests.
sys.exit(not result.wasSuccessful())
and I'd like to access command-line argument values from the class.
In here it's suggested that using __main__ isn't really a good approach.
Is there any better way of doing it, where individual tests could have access to argument values which were parsed already in main?
Testing:
python3 test.py
python3 test.py -v