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.py
pylenium.json
pytest.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
@pytest.fixture
- this decorator indicates that this function has a Setup and Teardowndef user():
- define the function normally.user
will be the name of the fixture to be used in testsEverything before the
yield
is executed before each testyield new_user
- returnsnew_user
and gives control back to the test. The rest of the function is not executed yetEverything after the
yield
is executed after each test
Use the Fixture
When this test is executed:
test - The test looks at its parameter list and calls the
py
fixturefixture -
user
yields the newly created usertest - line 2 is executed by navigating to
https://qap.dev
and 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.
Scope refers to the file's siblings and descendants.
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
/tests
directory (optional)You may want to make files easily identified as test files, so use
test_*.py
(optional)
These techniques help you and the Test Runner discover/find and execute your tests more easily, but they are not required. Do what works best for you and your team.
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
Classes
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)
You can have as many Test Classes and Test Functions as you want in a file
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 😄)
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.
Last updated