gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:30 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com