如何修复python构建错误“ValueError:没有这样的测试方法

时间:2013-02-27 07:09:16

标签: python unit-testing python-2.7

列出了我的python脚本:

==========================================

class ExampleTestCase(unittest.TestCase):
    capabilities = None

def setUp(self):
    self.driver = webdriver.Remote(desired_capabilities={ "browserName": broswer,      "platform": platform, "node": node })

def test_example(self):
    self.driver.get("www.360logica.com")
    self.assertEqual(self.driver.title, "360logica")

def tearDown(self):
    self.driver.quit()

if __name__ == "__main__":
    #unittest.main()
    args = sys.argv
    port = args[1]
    platform = args[2]
    broswer = args[3]
    suite = unittest.TestSuite()
    suite.addTest(ExampleTestCase("test_example"))
    runner = XMLTestRunner(file('results_ExampleTestCase_%s.xml' % (broswer), "w"))
    runner.run(suite)

============================================== < / p>

运行命令为:

$ ./python.exe Grid_1.py 5555 WINDOW firefox

============================================== < / p>

构建错误日志是:

$ ./python.exe Grid_1.py 5555 WINDOW firefox
Traceback (most recent call last):
      File "Grid_1.py", line 31, in <module>
        suite.addTest(ExampleTestCase("test_example"))
      File "C:\Python27\Lib\unittest\case.py", line 191, in __init__
        (self.__class__, methodName))
ValueError: no such test method in <class '__main__.ExampleTestCase'>: test_example

=============================================== ====

请帮帮我。我对这个构建错误很头疼,不知道如何修复它。

1 个答案:

答案 0 :(得分:0)

您有suite.addTest(ExampleTestCase("test_example")),但您的def超出了班级的范围(如果确实是您的缩进)。确保test_example是该类的一部分。

class ExampleTestCase(unittest.TestCase):
    capabilities = None

    def setUp(self):
        self.driver = webdriver.Remote(desired_capabilities={ "browserName": broswer, "platform": platform})

    def test_example(self):
        self.driver.get("www.360logica.com")
        self.assertEqual(self.driver.title, "360logica")

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    #unittest.main()
    args = sys.argv
    port = args[1]
    platform = args[2]
    broswer = args[3]
    suite = unittest.TestSuite()
    suite.addTest(ExampleTestCase("test_example"))
    runner = XMLTestRunner(file('results_ExampleTestCase_%s.xml' % (broswer), "w"))
    runner.run(suite)

python substring.py 5555 WINDOW firefox 这最终会将结果(如预期的那样)转储为results_ExampleTestCase_firefox.xml