Plugin for pytest that makes it simple to work with Azure Pipelines
Перейти к файлу
Anthony Shaw 27e5e41b54
Merge pull request #89 from Azure/tonybaloney-patch-1
Add Python 3.12 support and patch setuptools deprecated API
2023-10-06 19:00:20 +11:00
.azure-pipelines Remove Python 2 2020-06-09 12:22:06 +10:00
.github/workflows Update pytest versions 2023-10-06 15:49:36 +11:00
resources injects CSS styles into pytest coverage HTML files 2020-02-18 08:17:16 +01:00
tests start port away from setuptools deprecated lib 2023-10-06 18:05:49 +11:00
.gitignore More ignores 2019-08-28 14:25:48 +10:00
HISTORY.rst Update deps 2022-03-16 13:04:08 +11:00
LICENSE initial checkin 2019-01-19 11:19:40 +11:00
MANIFEST.in injects CSS styles into pytest coverage HTML files 2020-02-18 08:17:16 +01:00
README.rst Update README.rst 2022-07-26 09:43:40 +10:00
SECURITY.md Microsoft mandatory file 2023-01-27 20:57:01 +00:00
azure-pipelines.yml Force xUnit output 2020-06-09 12:51:40 +10:00
docker-compose.yml Rename ci to .azure-pipelines and fix xml output 2019-06-30 08:51:06 +10:00
pytest_azurepipelines.py Fix the loop for a second pass 2023-10-06 18:53:53 +11:00
screenshot.png Update newest version number and release notes 2019-10-25 10:33:40 +11:00
setup.py start port away from setuptools deprecated lib 2023-10-06 18:05:49 +11:00

README.rst

=====================
pytest-azurepipelines
=====================

.. image:: https://img.shields.io/pypi/v/pytest-azurepipelines.svg
    :target: https://pypi.org/project/pytest-azurepipelines
    :alt: PyPI version

.. image:: https://img.shields.io/pypi/pyversions/pytest-azurepipelines.svg
    :target: https://pypi.org/project/pytest-azurepipelines
    :alt: Python versions

.. image:: https://img.shields.io/pypi/dm/pytest-azurepipelines.svg
     :target: https://pypi.python.org/pypi/pytest-azurepipelines/
     :alt: PyPI download month

.. image:: https://anaconda.org/conda-forge/pytest-azurepipelines/badges/version.svg
     :target: https://anaconda.org/conda-forge/pytest-azurepipelines

Making Pytest easier to use with Microsoft Azure Pipelines.

Just run pytest with this plugin and see your test results in the Azure Pipelines UI!

----

Features:

* Formats the PyTest output to show test docstrings and module names instead of just test case names in the Azure Pipelines UI.
* Uploads test results automatically, no need for a separate test results upload command
* Displays the number of failed tests if there were failures as an error message in the UI
* Automatically formats code coverage and uploads coverage data if pytest-cov is installed
* Supports running inside a Docker container and automatically uploads test results

Installation
------------

You can install "pytest-azurepipelines" via `pip`_ from `PyPI`_::

    $ pip install pytest-azurepipelines

Usage
-----

This plugin requires no configuration.

Here is an example of installing the plugin and running the tests.

.. code-block:: yaml

  - script: |
      python -m pip install --upgrade pip
      pip install pytest pytest-azurepipelines
      pip install -e .
    displayName: 'Install dependencies'

  - script: |
      python -m pytest tests/
    displayName: 'pytest'

If you want to change the Azure Pipelines "Test Run Title", you can provide the `--test-run-title` flag with the run title.

.. code-block:: yaml

  - script: |
      pip install pytest pytest-azurepipelines
      pytest tests/ --test-run-title="Windows Test"

If you have long docstrings in your functions and want them to be shortened, you can use the `--napoleon-docstrings` flag:

.. code-block:: yaml
 
   - script: |
      pip install pytest pytest-azurepipelines
      pytest tests/ --test-run-title="Windows Test" --napoleon-docstrings

The directory where the html reports are searched for can be changed with the `--report-dir` flag. It defaults to `htmlcov`:

.. code-block:: yaml
 
   - script: |
      pip install pytest pytest-azurepipelines
      pytest tests/ --report-dir=custom_folder

Fixtures
--------

The following fixtures are made available by this plugin.

``record_pipelines_property``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Calling `record_pipelines_property(key: str, value: str)` will result in `Property` tags being added to the `test-case` for the related node. 

.. code-block:: python

    def test_basic(record_pipelines_property):
        record_pipelines_property("test", "value")
        assert 1 == 1

``add_pipelines_attachment``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Add an attachment to a node test-case by calling the `add_pipelines_attachment(path: str, description: str)` function with the filepath and a description.

Attachments can be viewed in the Azure Pipelines UI under the 'Attachments' tab for a test case.

.. code-block:: python

    def test_attachment(add_pipelines_attachment):
        pth = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'fixture.gif')
        add_pipelines_attachment(path, "peanut butter jelly time")
        assert 1 == 1

Using the automatic code coverage upload
----------------------------------------

From version 0.6.0, pytest will upload successful coverage data into a format that Azure supports and package
the htmlcov directory into a ZIP file as an artifact for the build.

To use this feature, add the `--cov` flag with (optional, but required) path to your code files and also ensure you add `--cov-report html` as an option.

.. code-block:: yaml
 
   - script: |
      pip install pytest pytest-azurepipelines pytest-cov
      pytest tests/ --cov my_project --cov-report html

To disable coverage upload, use the `--no-coverage-upload` flag.

Running in Docker
-----------------

The plugin automatically detects when running inside a docker
container. It will apply
the mappings to the path to report them back to Azure Pipelines using the path
from the host that has been bind mounted to the docker container. 

No configuration is required if bind mounting is
used to the path the pytest output is written to. Also ensure the files are
written using an account the host has access to, this can be done by supplying
the user and group of the host account to the run command.

.. code-block:: bash

    docker run --user "$(id -u):$(id -g)" ...

To disable docker discovery, use the `--no-docker-discovery` flag.

Contributing
------------

Contributions are very welcome. 

License
-------

Distributed under the terms of the MIT license, "pytest-azurepipelines" is free and open source software


Issues
------

If you encounter any problems, please `file an issue`_ along with a detailed description.

.. _`Apache Software License 2.0`: http://www.apache.org/licenses/LICENSE-2.0
.. _`cookiecutter-pytest-plugin`: https://github.com/pytest-dev/cookiecutter-pytest-plugin
.. _`file an issue`: https://github.com/tonybaloney/pytest-azurepipelines/issues
.. _`pytest`: https://github.com/pytest-dev/pytest
.. _`tox`: https://tox.readthedocs.io/en/latest/
.. _`pip`: https://pypi.org/project/pip/
.. _`PyPI`: https://pypi.org/project