【问题标题】:How to fix python build error "ValueError: no such test method in <class '__main__."如何修复python构建错误“ValueError:<class'__main__中没有这样的测试方法。”
【发布时间】:2013-02-27 07:09:16
【问题描述】:

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

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

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

【问题讨论】:

    标签: python unit-testing python-2.7


    【解决方案1】:

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

    【讨论】:

    • 嗨,戈登,当我尝试将 test_example 重新定义为类的一部分时,我仍然遇到该构建错误。但是无论我尝试什么,我都会一次又一次地遇到这个错误......太头疼了......作为专家,你能帮我def test_example的最佳位置在哪里吗?再次感谢。
    • 我不知道这里到底发生了什么,但我可以指出一些事情。在您的def setUp() 中,您调用"node": node,但节点从未在任何地方列出。修复此问题后,我可以使用您提供的命令运行它,这是我的输出:pastebin.com/hFWmxiUe
    • 节点是指测试内容将发送到的客户端机器。但是现在它对编译器没有用。可以去掉。我还看到了运行日志,您已经将其删除并在没有节点的情况下完全运行。因此,您确实将 test_example 重新定义为课程的一部分。我可以试试你的解决方法吗?我请你吃午饭。
    • 在设置中发布时尝试修复以删除节点。不知道怎么样。得到与我之前运行相同的错误。除了remove node: node,你还有其他的改变吗?
    • User@SC027042 ~/Python27/selenv/Scripts $ ./python.exe Grid_1.py 5555 WINDOW firefox Traceback(最近一次调用最后):文件“Grid_1.py”,第 32 行,在 suite.addTest(ExampleTestCase("test_example")) File "C:\Python27\Lib\unittest\case.py", line 191, in init (self.__class__, methodName)) ValueError : main.ExampleTestCase'> 中没有这样的测试方法:test_example
    猜你喜欢
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多