3. Project Structure with pytest
pytest uses specific naming conventions and project structure
Last updated
pytest uses specific naming conventions and project structure
Last updated
You should have created these in the , but they are required for Pylenium to do its magic.
conftest.py
pylenium.json
pytest.ini
Make sure these are at the Project Root (aka Workspace)
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.
@pytest.fixture
- this decorator indicates that this function has a Setup and Teardown
def user():
- define the function normally. user
will be the name of the fixture to be used in tests
Everything before the yield
is executed before each test
yield new_user
- returns new_user
and gives control back to the test. The rest of the function is not executed yet
Everything after the yield
is executed after each test
When this test is executed:
test - The test looks at its parameter list and calls the py
fixture
fixture - user
yields the newly created user
test - line 2 is executed by navigating to https://qap.dev
and then logging in with the new user
fixture - test is complete (doesn't matter if it passes or fails) and user_service.delete_user()
is executed
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
By now it might be obvious that pytest has specific naming conventions by default.
You may want to store your tests in a /tests
directory (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
You can group tests into Suites using Classes.
This is not the recommended approach for beginners
The class must start with the word Test
Test functions must have self
as their first parameter (since they are in a class)
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 π)
Test 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.