How to define test methods using Python Unittest

馋奶兔 提交于 2019-12-11 15:44:59

问题


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

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?


回答1:


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

  • 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()
    
  • 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")
    

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




回答2:


You write that the first function is ok, which I assume must be the setUp() function you are referring to (provided that in your code you indented correctly).

As Andersson comments, your unittest methods needs to begin with "test_" not "teste_". Providing "test_" is your way of telling unittest that this method should be tested.

In your unittest you probably also want to test something such as self.assertEqual(1,1) otherwise your tests will pass no matter what.

Next time please provide us with a more thorough description of what is wrong. How did you make the call to unittest? What error is python giving you? What result did you expect? Etc. It makes solving your problem much faster.

I encourage you to make a simple test first and ensure that it runs:

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)

Call this from your terminal:

>>>python -m unittest "name-of-test.py"


来源:https://stackoverflow.com/questions/52562186/how-to-define-test-methods-using-python-unittest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!