pyCharm命令行参数不起作用

时间:2019-08-29 19:24:01

标签: python pycharm

我已经阅读了很多关于此的文章,并尝试了所有方法,但是以某种方式我无法将命令行参数传递给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()

enter image description here

1 个答案:

答案 0 :(得分:1)

在不知道myTest.py中包含什么的情况下,这有点猜测,但是如果您在运行文件时调用unittest或测试运行程序,则参数将被解释为测试模块跑步。换句话说,unittest正在寻找名为ABCXYZ的Python测试模块。而且,如果ABC.pyXYZ.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