On the eve of the start of the " Python QA Engineer " course , a translation of useful material was prepared for future students and everyone interested in the topic of testing.
We also invite you to watch the gift demo lesson on the topic "QA Career".
There is a lot of debate in the testing community about how many asserts should be in one automated UI test. Some people think that there should be one assert per test, that is, each test should check only one element. Others are quite happy that their test checks several items at once.
Whichever approach you choose, I think it's safe to say that tests should remain clear, concise, readable, and of course, should be easy to maintain. Personally, I have no problems with multiple asserts in one test, since I focus on one functional area.
For example, let's take a registration form:
As part of testing the user registration form, I might want to test several things at once. Maybe I want to check that the message - congratulations on registration is displayed correctly, or maybe I will check that the user will be redirected to the login page after registration.
In such a case, I will not verify that the user can successfully log in and leave that for another test.
Problem
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.
- - " QA".