# Python Pytest

## Installation <a href="#installation" id="installation"></a>

{% hint style="info" %}
If you're starting from scratch, we need to create a python environment inside your project first and activate it
{% endhint %}

```
python -m venv env
env\Scripts\activate
```

Your project stracture should look like this:

<div align="left"><figure><img src="/files/xdEB6dDch3ib39qm9AEb" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
In order for Python to know our src folder we need to set the path to src
{% endhint %}

```
set PYTHONPATH=src
```

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

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

## Implementing Cloudbeat Reporting <a href="#implementing-cloudbeat-reporting" id="implementing-cloudbeat-reporting"></a>

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.

<figure><img src="/files/biiWiY1wmVfeRzVsVcpM" alt=""><figcaption></figcaption></figure>

## Creating a Python Pytest project in Cloudbeat <a href="#creating-a-java-junit-project-in-cloudbeat" id="creating-a-java-junit-project-in-cloudbeat"></a>

Coming soon!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cloudbeat.io/python-pytest.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
