Reputation: 569
I have read many posting on this and tried everything but somehow I can not pass command Line arguments to my python code in pyCharm. I have done the following
A) pls see first pic attached , when I run the code I get this error
C:\Automation\myTest\venv\Scripts\python.exe -s C:/Automation/myTest/myTest.py ABC XYZ
======================================================================
ERROR: ABC (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'ABC'
======================================================================
ERROR: XYZ (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'XYZ'
The I tried the same as shown in attached pic without -s option
C:\Automation\myTest\venv\Scripts\python.exe C:/Automation/myTest/myTest.py ABC XYZ
======================================================================
ERROR: ABC (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'ABC'
======================================================================
ERROR: XYZ (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'XYZ'
code
class Ktests(unittest.TestCase):
@classmethod
def setUpClass(self):
super(Ktests, self).setUpClass()
self.prepareInitalData(self)
@classmethod
def tearDownClass(self):
print('Tear Down')
super(Ktests, self).tearDownClass()
def prepareInitalData(self):
do stuff
def otherMethod(self):
do Other stuff
def test(self):
self.suites()
def suites(self):
runTest1()
....
if __name__ == '__main__':
unittest.main()
Upvotes: 1
Views: 543
Reputation: 4700
Without knowing what's in myTest.py
, this is a bit of a guess, but if you're invoking unittest
or a test runner when the file is run, then the parameters are interpreted as test modules to run. In other words, unittest
is looking for Python test modules called ABC
and XYZ
. And if ABC.py
and XYZ.py
don't exist, you'd get exactly the sort of error you're seeing.
If you want to have your own parameters, in addition to unittest
's expectations, you can modify your call to main()
by passing in arguments directly. For example if you want to use the first to arguments (after the program name) for yourself, and pass the rest to unittest
:
if __name__ == '__main__':
arg1, arg2 = sys.argv[1:3]
unittest.main(argv=sys.argv[3:])
This will assign the first to arguments to variables you can use, and then passes any others to unittest
. So you could make the call in your original question:
python myTest.py ABC XYZ
Or you can do that, plus run a specific test:
python myTest.py ABC XYZ path.to.test.module
https://docs.python.org/3/library/unittest.html#unittest.main
Upvotes: 1