3. Project Structure with pytest
pytest uses specific naming conventions and project structure
Pylenium Files
You should have created these in the previous step, but they are required for Pylenium to do its magic.
conftest.pypylenium.jsonpytest.ini
Make sure these are at the Project Root (aka Workspace)
conftest.py
pytest uses special functions called Fixtures to control the Setup and Teardown of tests and runs.
If you put any other custom functions or fixtures in this conftest.py, they will be overwritten when you upgrade Pylenium. Instead, create your own conftest.py file under your /tests directory.
Fixture Example
import pytest
@pytest.fixture
def user():
new_user = user_service.create()
yield new_user
user_service.delete(new_user.id)@pytest.fixture- this decorator indicates that this function has a Setup and Teardowndef user():- define the function normally.userwill be the name of the fixture to be used in testsEverything before the
yieldis executed before each testyield new_user- returnsnew_userand gives control back to the test. The rest of the function is not executed yetEverything after the
yieldis executed after each test
Use the Fixture
def test_my_website(py, login, user):
py.visit('https://qap.dev')
login.with(user)
...When this test is executed:
test - The test looks at its parameter list and calls the
pyfixturefixture -
useryields the newly created usertest - line 2 is executed by navigating to
https://qap.devand then logging in with the new userfixture - test is complete (doesn't matter if it passes or fails) and
user_service.delete_user()is executed
Folder Structure
The conftest.py file is used to store fixtures and make them available to any tests in their Scope.
Take a look at the following Project Structure
Project
conftest.py # 1
pylenium.json
api_tests
conftest.py # 2
test_rest_api.py
ui_tests
conftest.py # 3
test_google.py
test_google.py would have access to fixtures in conftest.py #1 and conftest.py #3
test_rest_api.py would have access to fixtures in conftest.py #1 and conftest.py #2
Test Naming Conventions
By now it might be obvious that pytest has specific naming conventions by default.
Folders and Files
You may want to store your tests in a
/testsdirectory (optional)You may want to make files easily identified as test files, so use
test_*.py(optional)
pytest can run tests based off of directories or files, so you can group tests into Suites this way.
Project
tests
ui
test_login.py
test_checkout.py
api
test_payment.py
test_user.py
unit
# run all tests
$ pytest tests
# run tests in ui directory
$ pytest tests/ui
# run only the payment api tests
$ pytest tests/api/test_payment.pyClasses
You can group tests into Suites using Classes.
This is not the recommended approach for beginners
def TestCheckout:
def test_with_visa(self, py):
# test code
def test_with_mastercard(self, py):
# test codeThe class must start with the word
TestTest functions must have
selfas their first parameter (since they are in a class)
Test Functions
Tests do NOT need to be in a Test Class. They can exist by themselves in a file and makes the tests and overall file look much cleaner.
RECOMMEND this approach for working with Pylenium for beginners (and everyone else really 😄)
def test_with_visa(py):
# test_code
def test_with_mastercard(py):
# test_codeTest names must start with
test_, but can have anything else after that
Tests should not share data or state.
Tests should be modular, deterministic, and meaningful
Pylenium is architected in a way that makes test design easy and intuitive but also gives you a lot of things for free. The framework is already designed to be scaled with containerized solutions like Docker and Kubernetes.
Last updated