[英]Running all selenium tests inside a test case
I have multiple tests inside one test case but I am noticing that it is only running the first test 我在一个测试用例中有多个测试,但我注意到它仅在运行第一个测试
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()
Whenever I run this it only runs 每当我运行它时,它只会运行
def test_Google(self)
and nothing after that. 在那之后什么也没有。 am I doing something wrong? 难道我做错了什么?
Methods must begin with 'test'
to be automatically run. 方法必须以'test'
开头才能自动运行。
Per the docs : 根据文档 :
A testcase is created by subclassing unittest.TestCase. 通过子类化unittest.TestCase创建一个测试用例。 The three individual tests are defined with methods whose names start with the letters
test
. 这三个单独的测试使用名称以字母test
开头的方法定义。 This naming convention informs the test runner about which methods represent tests. 该命名约定将告知测试运行者哪些方法表示测试。 (my emphasis) (我的重点)
The TestLoader
is responsible for loading test and returning them wrapped in a TestSuite
. TestLoader
负责加载测试,并将其包装在TestSuite
返回。 It uses 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))
...
Thus, the attrname.startswith(prefix)
checks if the method name begins with 'test'
. 因此, attrname.startswith(prefix)
检查方法名称是否以'test'
开头。
As an alternative to what @unubtu noted: 替代@unubtu指出的内容:
you can use a nose
test runner and mark a method with @istest
decorator : 您可以使用nose
测试 @istest
器并使用@istest
装饰器标记方法:
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()
Besides, here is a pretty good overview of the unittest
test discovery: 此外,这里是一个相当不错的概述unittest
的测试发现:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.