Skip to main content
 首页 » 编程设计

python之如何修复 python 构建错误 "ValueError: no such test method in class ' __main__。”

2025年05月04日16jyk

列出了我的 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) 

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

运行命令为:

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

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

构建错误日志为:

$ ./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 

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

请帮帮我。我对那个构建错误感到非常头疼,不知道如何修复它。

请您参考如下方法:

你有 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 火狐 这最终将结果(如预期的那样)转储为 results_ExampleTestCase_firefox.xml