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

conftest.py

pytest uses special functions called Fixtures to control the Setup and Teardown of tests and runs.

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

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

Classes

You can group tests into Suites using Classes.

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

  • Test names must start with test_, but can have anything else after that

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