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

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 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

Use the Fixture

test_*.py file
def test_my_website(py, login, user):
    py.visit('https://qap.dev')
    login.with(user)
    ...

When this test is executed:

  1. test - The test looks at its parameter list and calls the py fixture

  2. fixture - user yields the newly created user

  3. test - line 2 is executed by navigating to https://qap.dev and then logging in with the new user

  4. fixture - 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

# run all tests
$ pytest tests

# run tests in ui directory
$ pytest tests/ui

# run only the payment api tests
$ pytest tests/api/test_payment.py

Classes

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 code
  • 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_checkout.py
def test_with_visa(py):
    # test_code
    
def test_with_mastercard(py):
    # test_code
  • 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