# 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="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2Fls1ps8XHOzvKrCipPUER%2Fimage.png?alt=media&#x26;token=6795e39d-bf69-49c9-a2e3-5dd598eba3a9" 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="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2Ff7iCuzSGvBxru6CxVH8P%2Fimage.png?alt=media&#x26;token=21fcdb55-b627-4ccc-b64d-fb37dc53569b" 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!
