| 
 | 1 | +# ReportPortal integration for pytest framework  | 
 | 2 | + | 
 | 3 | +Pytest plugin for reporting test results of the Pytest to the ReportPortal.  | 
 | 4 | + | 
 | 5 | +> **DISCLAIMER**: We use Google Analytics for sending anonymous usage information such as agent's and client's names,  | 
 | 6 | +> and their versions after a successful launch start. This information might help us to improve both ReportPortal  | 
 | 7 | +> backend and client sides. It is used by the ReportPortal team only and is not supposed for sharing with 3rd parties.  | 
 | 8 | +
  | 
 | 9 | +[](https://pypi.python.org/pypi/pytest-reportportal)  | 
 | 10 | +[](https://pypi.org/project/pytest-reportportal)  | 
 | 11 | +[](https://github.com/reportportal/agent-python-pytest/actions/workflows/tests.yml)  | 
 | 12 | +[](https://codecov.io/gh/reportportal/agent-python-pytest)  | 
 | 13 | +[](https://slack.epmrpp.reportportal.io/)  | 
 | 14 | +[](http://stackoverflow.com/questions/tagged/reportportal)  | 
 | 15 | +[](http://reportportal.io?style=flat)  | 
 | 16 | + | 
 | 17 | +## Installation  | 
 | 18 | + | 
 | 19 | +To install pytest plugin execute next command in a terminal:  | 
 | 20 | + | 
 | 21 | +```bash  | 
 | 22 | +pip install pytest-reportportal  | 
 | 23 | +```  | 
 | 24 | + | 
 | 25 | +Look through the `CONTRIBUTING.rst` for contribution guidelines.  | 
 | 26 | + | 
 | 27 | +## Configuration  | 
 | 28 | + | 
 | 29 | +Prepare the config file `pytest.ini` in root directory of tests or specify any one using pytest command line option:  | 
 | 30 | + | 
 | 31 | +```bash  | 
 | 32 | +py.test -c config.cfg  | 
 | 33 | +```  | 
 | 34 | + | 
 | 35 | +The `pytest.ini` file should have next mandatory fields:  | 
 | 36 | + | 
 | 37 | +- `rp_api_key` - value could be found in the User Profile section  | 
 | 38 | +- `rp_project` - name of project in ReportPortal  | 
 | 39 | +- `rp_endpoint` - address of ReportPortal Server  | 
 | 40 | + | 
 | 41 | +Example of `pytest.ini`:  | 
 | 42 | + | 
 | 43 | +```text  | 
 | 44 | +[pytest]  | 
 | 45 | +rp_api_key = fb586627-32be-47dd-93c1-678873458a5f  | 
 | 46 | +rp_endpoint = http://192.168.1.10:8080  | 
 | 47 | +rp_project = user_personal  | 
 | 48 | +rp_launch = AnyLaunchName  | 
 | 49 | +rp_launch_attributes = 'PyTest' 'Smoke'  | 
 | 50 | +rp_launch_description = 'Smoke test'  | 
 | 51 | +rp_ignore_attributes = 'xfail' 'usefixture'  | 
 | 52 | +```  | 
 | 53 | + | 
 | 54 | +- The `rp_api_key` can also be set with the environment variable `RP_API_KEY`. This will override the value set for `rp_api_key` in pytest.ini  | 
 | 55 | + | 
 | 56 | +There are also optional parameters:  | 
 | 57 | +https://reportportal.io/docs/log-data-in-reportportal/test-framework-integration/Python/pytest/  | 
 | 58 | + | 
 | 59 | +## Examples  | 
 | 60 | + | 
 | 61 | +For logging of the test item flow to ReportPortal, please, use the python logging handler provided by plugin like  | 
 | 62 | +below:  | 
 | 63 | + | 
 | 64 | +in `conftest.py`:  | 
 | 65 | + | 
 | 66 | +```python  | 
 | 67 | +import logging  | 
 | 68 | + | 
 | 69 | +import pytest  | 
 | 70 | + | 
 | 71 | +from reportportal_client import RPLogger  | 
 | 72 | + | 
 | 73 | + | 
 | 74 | +@pytest.fixture(scope="session")  | 
 | 75 | +def rp_logger():  | 
 | 76 | +    logger = logging.getLogger(__name__)  | 
 | 77 | +    logger.setLevel(logging.DEBUG)  | 
 | 78 | +    logging.setLoggerClass(RPLogger)  | 
 | 79 | +    return logger  | 
 | 80 | +```  | 
 | 81 | + | 
 | 82 | +in tests:  | 
 | 83 | + | 
 | 84 | +```python  | 
 | 85 | +# In this case only INFO messages will be sent to the ReportPortal.  | 
 | 86 | +def test_one(rp_logger):  | 
 | 87 | +    rp_logger.info("Case1. Step1")  | 
 | 88 | +    x = "this"  | 
 | 89 | +    rp_logger.info("x is: %s", x)  | 
 | 90 | +    assert 'h' in x  | 
 | 91 | + | 
 | 92 | +    # Message with an attachment.  | 
 | 93 | +    import subprocess  | 
 | 94 | +    free_memory = subprocess.check_output("free -h".split())  | 
 | 95 | +    rp_logger.info(  | 
 | 96 | +        "Case1. Memory consumption",  | 
 | 97 | +        attachment={  | 
 | 98 | +            "name": "free_memory.txt",  | 
 | 99 | +            "data": free_memory,  | 
 | 100 | +            "mime": "application/octet-stream",  | 
 | 101 | +        },  | 
 | 102 | +    )  | 
 | 103 | + | 
 | 104 | +    # This debug message will not be sent to the ReportPortal.  | 
 | 105 | +    rp_logger.debug("Case1. Debug message")  | 
 | 106 | +```  | 
 | 107 | + | 
 | 108 | +## Launching  | 
 | 109 | + | 
 | 110 | +To run test with ReportPortal you must provide `--reportportal` flag:  | 
 | 111 | + | 
 | 112 | +```bash  | 
 | 113 | +py.test ./tests --reportportal  | 
 | 114 | +```  | 
 | 115 | + | 
 | 116 | +Check the documentation to find more detailed information about how to integrate pytest with ReportPortal using the  | 
 | 117 | +agent:  | 
 | 118 | +https://reportportal.io/docs/log-data-in-reportportal/test-framework-integration/Python/pytest/  | 
 | 119 | + | 
 | 120 | +## Copyright Notice  | 
 | 121 | + | 
 | 122 | +Licensed under the [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) license (see the LICENSE file).  | 
0 commit comments