Pytest-check assert método de erros sem bloqueio

Na véspera do início do curso " Python QA Engineer " , uma tradução de material útil foi preparada para os futuros alunos e todos os interessados ​​no tema dos testes.



Também o convidamos a assistir a uma aula de demonstração sobre o tópico "Carreira de controle de qualidade".










Há muito debate na comunidade de testes sobre quantas declarações devem estar em um teste de IU automatizado. Algumas pessoas acham que deveria haver uma assert por teste, ou seja, cada teste deveria verificar apenas um elemento. Outros ficam muito felizes com o fato de seu teste verificar vários itens de uma vez. 





Seja qual for a abordagem que você escolher, acho que é seguro dizer que os testes devem permanecer claros, concisos, legíveis e, claro, devem ser fáceis de manter. Pessoalmente, não tenho problemas com várias afirmações em um teste, pois estou me concentrando em uma área funcional.





Por exemplo, vamos pegar um formulário de registro:





, , , . , — , , . 





, , .





assert Python , . . , . , , , assert- .





-. , , , , , , assert.





: Pytest-check

Pytest-check ( ) – Pytest, assert- pass/fail. , 3 assert- fail, Pytest-check 2. , , fail.





Python OpenSDK TestProject Pytest, pytest Selenium, TestProject. HowQA,





, Pytest-check.





Selenium

. : https://docket-test.herokuapp.com/register





import selenium.webdriver as webdriver
from selenium.webdriver.common.by import By
def test_register_user():
    # Arrange
    url = "https://docket-test.herokuapp.com/register"
    # set the driver instance
    driver = webdriver.Chrome()
    # browse to the endpoint
    driver.get(url)
    # maximise the window
    driver.maximize_window()
    # Act
    # Complete registration form
    # enter username value
    driver.find_element(By.ID, "username").send_keys("Ryan")
    # enter email value
    driver.find_element(By.ID, "email").send_keys("Test@email.com")
    # enter password value
    driver.find_element(By.ID, "password").send_keys("12345")
    # enter repeat password value
    driver.find_element(By.ID, "password2").send_keys("12345")
    # click register button
    driver.find_element(By.ID, "submit").click()
      
      



, . assert:





# Assert
# confirm registration has been successful
# check if congratulations message contains the correct text
message = driver.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]").text
assert message == "Congratulations, you are now registered"
# check user is routed to login page
current_url = driver.current_url
assert current_url == "https://docket-test.herokuapp.com/login"
      
      



, :





, , assert- fail? , , :





# Assert
# confirm registration has been successful
# check if congratulations message contains the correct text
message = driver.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]").text
assert message == "Well done, You've Registered"
# check user is routed to login page
current_url = driver.current_url
assert current_url == "https://docket-test.herokuapp.com/register"
driver.quit()
      
      



, , URL, , , fail:





, assert. , , , , - …





, - , URL . , fail, . . .





«Congratulations, you are now registered», :





! , - URL.





, , , . , , Pytest-check.





Pytest-Check

pytest-check pip install pytest-check



. pytest-check, .





import pytest_check as check
      
      



, , assert-. assert, pytest-check . 





check.equal



, :





check.equal(message, "Congratulations, you are now registered1")
      
      



URL-, , check.is_in



.





check.is_in("login", current_url)
      
      



:





import selenium.webdriver as webdriver
from selenium.webdriver.common.by import By
import pytest_check as check
def test_register_user():
    # Arrange
    url = "https://docket-test.herokuapp.com/register"
    # set the driver instance
    driver = webdriver.Chrome()
    # browse to the endpoint
    driver.get(url)
    # maximise the window
    driver.maximize_window()
    # Act
    # Complete registration form
    # enter username value
    driver.find_element(By.ID, "username").send_keys("Ryan8")
    # enter email value
    driver.find_element(By.ID, "email").send_keys("Test@email8.com")
    # enter password value
    driver.find_element(By.ID, "password").send_keys("12345")
    # enter repeat password value
    driver.find_element(By.ID, "password2").send_keys("12345")
    # click register button
    driver.find_element(By.ID, "submit").click()
    # Assert
    # confirm registration has been successful
    # check if congratulations message contains the correct text
    message = driver.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]").text
    check.equal(message, "Congratulations, you are now registered")
    # check user is routed to login page
    current_url = driver.current_url
    check.is_in("login", current_url)
    driver.quit()
      
      



, . .





! , , fail. , :





# check if congratulations message contains the correct text
message = driver.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]").text
check.equal(message, "Congratulations, you are now registered!")
# check user is routed to login page
current_url = driver.current_url
check.is_in("1", current_url)
      
      



.





, , , , fail : , , URL. pytest, , - , fail. 





, pass.





Pytest-check. .






- "Python QA Engineer".









- - " QA".












All Articles