如何使用 Python Unittest 定义测试方法
How to define test methods using Python Unittest
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class CorrecaoEfetivaNota(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome('/Users/r13/dev/chromedriver')
def teste_login_avaliador(self):
driver = self.driver
driver.get("")
cpf = driver.find_element_by_xpath('//input[@placeholder="CPF"]')
cpf.send_keys("")
password = driver.find_element_by_xpath('//input[@placeholder="SENHA"]')
password.send_keys("")
login = driver.find_element_by_tag_name('button')
login.click()
driver.implicitly_wait(3)
def teste_buscar_mais_um(self):
driver = self.driver
buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
buscar.click()
def tearDown(self):
self.driver.close()
我正在尝试在 Python 中编写此测试,第一个函数没问题,但 class 中的第二个函数未在测试中执行。我该如何组织?
你写的第一个函数没问题,我想这一定是你指的setUp()
函数(前提是你的代码缩进正确)。
正如 Andersson 评论的那样,您的单元测试方法需要以 "test_" 而不是 "teste_" 开头。提供 "test_" 是告诉 unittest
应该测试此方法的方式。
在您的 unittest
中,您可能还想测试诸如 self.assertEqual(1,1)
之类的东西,否则无论如何您的测试都会通过。
下次请向我们提供更详尽的错误说明。您是如何调用 unittest 的? python 给你什么错误?你期望什么结果?等等。它可以更快地解决您的问题。
我鼓励你先做一个简单的测试并确保它运行:
import unittest
class TestingUnittest(unittest.TestCase):
def setUp(self):
print("SetUp called")
def tearDown(self):
print("tearDown called")
def test_Method(self):
print("Testing 1==1")
self.assertEqual(1,1)
从您的终端调用:
>>>python -m unittest "name-of-test.py"
在使用 Python 和 unittest 模块以及 Selenium 时,您必须考虑以下几个事实:
- class 和
test_method
的缩进不同。
- 而不是
driver.close()
始终在 tearDown(){}
方法中调用 driver.quit()
来关闭和销毁 WebDriver 和 Web 客户端 个优雅的实例。
- 如果您使用 unittest 模块,您必须调用
__main__
.
这是您自己的代码,稍作修改后将执行 第一个方法 teste_login_avaliador()
以及 第二种方法 teste_buscar_mais_um()
Class CorrecaoEfetivaNota()
:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class CorrecaoEfetivaNota(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
def teste_login_avaliador(self):
driver = self.driver
driver.get("http://d3dyod5mwyu6xk.cloudfront.net/")
cpf = driver.find_element_by_xpath('//input[@placeholder="CPF"]')
cpf.send_keys("27922797885")
password = driver.find_element_by_xpath('//input[@placeholder="SENHA"]')
password.send_keys("enccejaregular")
login = driver.find_element_by_tag_name('button')
login.click()
driver.implicitly_wait(3)
def teste_buscar_mais_um(self):
driver = self.driver
buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
buscar.click()
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
注意:尽管 test_methods 都被调用了,但您仍然会遇到以下异常:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button"}
行:
buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
按照 用例 的实际 测试步骤 可以轻松解决此异常,如果需要,您可以提出新的 question/ticket.
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class CorrecaoEfetivaNota(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome('/Users/r13/dev/chromedriver')
def teste_login_avaliador(self):
driver = self.driver
driver.get("")
cpf = driver.find_element_by_xpath('//input[@placeholder="CPF"]')
cpf.send_keys("")
password = driver.find_element_by_xpath('//input[@placeholder="SENHA"]')
password.send_keys("")
login = driver.find_element_by_tag_name('button')
login.click()
driver.implicitly_wait(3)
def teste_buscar_mais_um(self):
driver = self.driver
buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
buscar.click()
def tearDown(self):
self.driver.close()
我正在尝试在 Python 中编写此测试,第一个函数没问题,但 class 中的第二个函数未在测试中执行。我该如何组织?
你写的第一个函数没问题,我想这一定是你指的setUp()
函数(前提是你的代码缩进正确)。
正如 Andersson 评论的那样,您的单元测试方法需要以 "test_" 而不是 "teste_" 开头。提供 "test_" 是告诉 unittest
应该测试此方法的方式。
在您的 unittest
中,您可能还想测试诸如 self.assertEqual(1,1)
之类的东西,否则无论如何您的测试都会通过。
下次请向我们提供更详尽的错误说明。您是如何调用 unittest 的? python 给你什么错误?你期望什么结果?等等。它可以更快地解决您的问题。
我鼓励你先做一个简单的测试并确保它运行:
import unittest
class TestingUnittest(unittest.TestCase):
def setUp(self):
print("SetUp called")
def tearDown(self):
print("tearDown called")
def test_Method(self):
print("Testing 1==1")
self.assertEqual(1,1)
从您的终端调用:
>>>python -m unittest "name-of-test.py"
在使用 Python 和 unittest 模块以及 Selenium 时,您必须考虑以下几个事实:
- class 和
test_method
的缩进不同。 - 而不是
driver.close()
始终在tearDown(){}
方法中调用driver.quit()
来关闭和销毁 WebDriver 和 Web 客户端 个优雅的实例。 - 如果您使用 unittest 模块,您必须调用
__main__
. 这是您自己的代码,稍作修改后将执行 第一个方法
teste_login_avaliador()
以及 第二种方法teste_buscar_mais_um()
ClassCorrecaoEfetivaNota()
:import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys class CorrecaoEfetivaNota(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe') def teste_login_avaliador(self): driver = self.driver driver.get("http://d3dyod5mwyu6xk.cloudfront.net/") cpf = driver.find_element_by_xpath('//input[@placeholder="CPF"]') cpf.send_keys("27922797885") password = driver.find_element_by_xpath('//input[@placeholder="SENHA"]') password.send_keys("enccejaregular") login = driver.find_element_by_tag_name('button') login.click() driver.implicitly_wait(3) def teste_buscar_mais_um(self): driver = self.driver buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button") buscar.click() def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
注意:尽管 test_methods 都被调用了,但您仍然会遇到以下异常:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button"}
行:
buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
按照 用例 的实际 测试步骤 可以轻松解决此异常,如果需要,您可以提出新的 question/ticket.