Python Pytest

This example project shows how to run Python Pytest with Cloudbeat integration. It’s a simple way to get started with running your tests in the cloud and viewing results in real time.

Installation

If you're starting from scratch, we need to create a python environment inside your project first and activate it

python -m venv env
env\Scripts\activate

Your project stracture should look like this:

In order for Python to know our src folder we need to set the path to src

set PYTHONPATH=src

Now we need to include the CloudBeat modules in our requirements.txt:

cloudbeat-common
cloudbeat-pytest
cloudbeat-selenium
pytest
pytest-html
pytest-metadata
pytest-xdist
python-dotenv
webdriver-manager
requests
selenium
uuid

To install it, use:

pip install -r requirements.txt

We can also install the CloudBeat modules manually:

pip install -e C:\Your Folder\cb-kit-common
pip install -e C:\Your Folder\cb-kit-selenium
pip install -e C:\Your Folder\cb-kit-pytest

Implementing Cloudbeat Reporting

We need to start by importing the Cloudbeat modules in our confitest.py file:

import uuid
import pytest
from selenium import webdriver
from cloudbeat_common.models import CbConfig
from cloudbeat_common.reporter import CbTestReporter
from cloudbeat_selenium.wrapper import CbSeleniumWrapper

Then, create a custom config for CloudBeat reporter:

@pytest.fixture(scope="module")
def cb_config():
    """Prepare configuration class for further CB reporter initialization."""
    config = CbConfig()
    config.run_id = str(uuid.uuid4())
    config.instance_id = str(uuid.uuid4())
    config.project_id = str(uuid.uuid4())
    config.capabilities = {"browserName": "chrome"}
    return config

@pytest.fixture(scope="module")
def cb_reporter(cb_config):
    reporter = CbTestReporter(cb_config)
    return reporter

Finally, wrap our driver with the CloudBeat reporter:

@pytest.fixture()
def setup(cb_reporter):
    driver = webdriver.Chrome()
    wrapper = CbSeleniumWrapper(cb_reporter)
    wrapped_driver = wrapper.wrap(driver)
    yield wrapped_driver
    driver.quit()

To test it locally, we can use several commands:

# run everything
pytest 

# run parallel tests
pytest -n 4

# run a single test
pytest -v -s tests/test_login.py 

After the test is finished, you should see a CB_TEST_RESULTS.json file which captured our steps.

Creating a Python Pytest project in Cloudbeat

Coming soon!

Last updated

Was this helpful?