Python 在测试用例中运行所有selenium测试

Python 在测试用例中运行所有selenium测试,python,selenium,selenium-webdriver,webdriver,Python,Selenium,Selenium Webdriver,Webdriver,我在一个测试用例中有多个测试,但我注意到它只运行第一个测试 import unittest from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support.ui import WebDriverWait class Test(unittest.TestCase): d

我在一个测试用例中有多个测试,但我注意到它只运行第一个测试

import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait

class Test(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = "http://www.example.com/"
    def test_Google(self):
        driver = self.driver
        driver.implicitly_wait(10)
        driver.get(self.base_url)
    def fill_contact(self):
        driver.find_element_by_xpath('//a[contains(.,"Contact")]').click()
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@type="submit"][@value="Send"]').click()

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

if __name__ == "__main__":
    unittest.main()
每当我运行这个时,它只运行

def test_Google(self)
之后就什么都没有了。我做错什么了吗?

方法必须以“test”开头才能自动运行

:

testcase是通过子类化unittest.testcase创建的。三个 单个测试使用名称以开头的方法定义 字母测试。此命名约定通知测试运行程序 哪些方法代表测试。我的重点

TestLoader负责加载测试并将其包装在TestSuite中返回。它使用:

因此,attrname.startswithprefix检查方法名称是否以“test”开头。

方法必须以“test”开头才能自动运行

:

testcase是通过子类化unittest.testcase创建的。三个 单个测试使用名称以开头的方法定义 字母测试。此命名约定通知测试运行程序 哪些方法代表测试。我的重点

TestLoader负责加载测试并将其包装在TestSuite中返回。它使用:


因此,attrname.startswithprefix检查方法名称是否以“test”开头。

作为@unubtu注意到的内容的替代:

您可以使用和标记具有以下内容的方法:

此外,下面是unittest发现的一个非常好的概述:


作为@unubtu指出的替代方案:

您可以使用和标记具有以下内容的方法:

此外,下面是unittest发现的一个非常好的概述:

class TestLoader(object):
    testMethodPrefix = 'test'
    def getTestCaseNames(self, testCaseClass):
        """Return a sorted sequence of method names found within testCaseClass
        """
        def isTestMethod(attrname, testCaseClass=testCaseClass,
                         prefix=self.testMethodPrefix):
            return attrname.startswith(prefix) and \
                hasattr(getattr(testCaseClass, attrname), '__call__')
        testFnNames = filter(isTestMethod, dir(testCaseClass))
        ...
from nose.tools import istest

class Test(unittest.TestCase):
    ...

    @istest
    def fill_contact(self):
        driver.find_element_by_xpath('//a[contains(.,"Contact")]').click()
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@type="submit"][@value="Send"]').click()