00:04:35.160 - 00:08:40.079
[INFO] [2021-06-22 18:15:35.16] [1793] [11586] - [1793] [11586] - [1793] [11586] - [1793] [11586]
# ------------------------- START TEST -------------------------
python -m pytest /path/to/your/pytest/file.py
```
Here's an example of how to use pytest to test a Python script:
1. Open your terminal or command prompt.
2. Navigate to the directory where your Python script is located using `cd`.
3. Run `pip install pytest` to install the pytest package if it hasn't already been installed.
4. Create a file named `conftest.py` in the same directory as your Python script with the following contents:
```python
import os
def add_logs(config, log_file='results.log'):
log_dir = os.path.join('logs', config['test_env']['name'])
if not os.path.exists(log_dir):
os.makedirs(log_dir)
with open(os.path.join(log_dir, log_file), 'a') as f:
f.write('------------------------- START TEST -------------------------
')
f.write(f'{config["test_env"]["name"]} - {config["test_env"]["version"]}
')
f.write('------------------------- END TEST -------------------------
')
```
5. Open your Python script and add the following imports:
```python
import pytest
from config import *
```
6. Add a test function to your script with the `@pytest.mark.test` decorator:
```python
def test_my_function():
assert my_function(1, 2) == 3
```
7. Save and run the test using `pytest`. The output should include the logs you specified in `conftest.py`:
```yaml
============================= test session starts ==============================
collected 1 item
test_my_function.py .. [100%]
============================== 1 passed in 0.01s ===============================
------------------------- END TEST -------------------------
```
8. You can run the test again using `pytest` to see if any changes were made:
```yaml
============================= test session starts ==============================
collected 1 item
test_my_function.py .. [100%]
============================== 1 passed in 0.02s ===============================
------------------------- END TEST -------------------------
```
That's it! You have now used pytest to test your Python script and log the results.