python - 在测试用例中运行所有 selenium 测试

标签 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):

    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' 开头才能自动运行。

Per the docs :

A testcase is created by subclassing unittest.TestCase. The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests. (my emphasis)


TestLoader 负责加载测试并将它们包装在 TestSuite 中返回。它使用 this method to identify tests :

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))
        ...

因此,attrname.startswith(prefix) 检查方法名称是否以 'test' 开头。

关于python - 在测试用例中运行所有 selenium 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27696556/

相关文章:

python - 如何使用 python 中的 join 将以下代码输出转换为一行。目前,对于两个单词输入,我得到两行输出

python - HTML 表格特定行抓取

json - 从网站的 GET 请求中获取 queryString - 替代工具然后是 Selenium

javascript - 单击具有 onclick javascript 属性的 href 按钮

java - 如何在 Serenity BDD Jbehave 中按特定顺序执行故事文件

python - 使用 Django 和 Steam 后端实现 python-social-auth 的正确方法

python - 如何使用 python 在 Windows 7 中制作屏幕截图?

python - 如果日期是周末 - 自动重新运行该函数

python - 在 mac 上使用 python 和 selenium 使用默认用户配置文件打开 chrome

Java Selenium - 从 Google map 实时交通 View 中选择一个元素