如何使用Python Unittest定义测试方法 [英] How to define test methods using Python Unittest

查看:181
本文介绍了如何使用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编写此测试,第一个功能还可以,但是类中的第二个功能未在测试中执行.我该如何组织呢?

I'm trying to write this tests in Python, the first function is ok, but the second one inside the class is not being executed in the tests. How can I organize this?

推荐答案

在使用Python和 Selenium unittest 模块时,您必须考虑以下几个事实:

While working with Python and unittest module with Selenium you have to consider a few facts as follows :

  • class test_method的缩进不同.
  • 总是使用tearDown(){}方法中的driver.quit()而不是driver.close()来关闭&优雅地销毁 WebDriver Web Client 实例.
  • 如果您使用的是 unittest 模块,则必须调用 __main__ .
  • 这是您自己的代码,其中进行了必要的细微修改,这些代码将执行 first方法 teste_login_avaliador() second方法 Class CorrecaoEfetivaNota() 中的em> teste_buscar_mais_um() :

  • Indentation for class and test_method are different.
  • Instead of driver.close() always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
  • If you are using unittest module you have to call the __main__.
  • Here is your own code with the required minor modifications which will execute the first method teste_login_avaliador() as well as the second method teste_buscar_mais_um() within the 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 都被调用了,您仍然会遇到以下异常:

  • Note: Though both the test_methods are being called still you will face the following exception:

    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"}
    

  • 在此行:

  • At the line:

    buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
    

    按照您的用例的实际 Test步骤可以轻松解决此异常,并且如果需要,您可以提出新的问题/票证.

    This exception can be solved easily following the actual Test Steps of your usecase and if required you can raise a new question/ticket.

    这篇关于如何使用Python Unittest定义测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆