我已经阅读了很多关于此的文章,并尝试了所有方法,但是以某种方式我无法将命令行参数传递给pyCharm中的python代码。我已经完成以下
A)请参阅所附的第一张图片,当我运行代码时出现此错误
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'
我尝试了与不带-s选项的所附图片相同的显示
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'
代码
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()
答案 0 :(得分:1)
在不知道myTest.py
中包含什么的情况下,这有点猜测,但是如果您在运行文件时调用unittest
或测试运行程序,则参数将被解释为测试模块跑步。换句话说,unittest
正在寻找名为ABC
和XYZ
的Python测试模块。而且,如果ABC.py
和XYZ.py
不存在,那么您将得到确切的错误信息。
如果您希望拥有自己的参数,除了unittest
的期望之外,还可以通过直接传递参数来修改对main()
的调用。例如,如果您想自己使用第一个to参数(在程序名称之后),然后将其余参数传递给unittest
:
if __name__ == '__main__':
arg1, arg2 = sys.argv[1:3]
unittest.main(argv=sys.argv[3:])
这会将第一个参数分配给您可以使用的变量,然后将其他参数传递给unittest
。因此,您可以在原始问题中拨打电话:
python myTest.py ABC XYZ
或者您可以执行此操作,然后运行特定的测试:
python myTest.py ABC XYZ path.to.test.module
https://docs.python.org/3/library/unittest.html#unittest.main