=============
pytest-replay
=============

.. image:: https://travis-ci.org/nicoddemus/pytest-replay.svg?branch=master
    :target: https://travis-ci.org/nicoddemus/pytest-replay
    :alt: See Build Status on Travis CI

.. image:: https://ci.appveyor.com/api/projects/status/github/nicoddemus/pytest-replay?branch=master
    :target: https://ci.appveyor.com/project/nicoddemus/pytest-replay/branch/master
    :alt: See Build Status on AppVeyor

Saves shell scripts that allow re-execute previous pytest runs to reproduce crashes or flaky tests

----

This `Pytest`_ plugin was generated with `Cookiecutter`_ along with `@hackebrot`_'s `Cookiecutter-pytest-plugin`_ template.


Features
--------

This plugin helps to reproduce random or flaky behavior when running tests with xdist: because xdist executes order
in a non-predictable manner, sometimes it is hard to reproduce a behavior seen in CI locally.

This plugin introduces a new ``--replay-script-dir`` flag where shell scripts will be recorded containing a footprint
of the tests executed by each worker during the session. For example::

    $ pytest -n auto --replay-script-dir build/tests/replay

Will now generate platform specific shell-scripts (``.bat`` for Windows and ``.sh`` for Linux) in the
``build/tests/replay`` directory which record each test executed by each worker, in a ready-to-use command-line
to reproduce it later. For example, it will generate a shell script for worker ``gw1`` named ``.pytest-replay-gw1.sh``
with this contents:

.. code-block:: bash

    # generated by pytest-replay
    pytest $* \
      test_foo.py::test[1] \
      test_foo.py::test[3] \
      test_foo.py::test[5] \
      test_foo.py::test[7] \
      test_foo.py::test[8] \

So to (hopefully) reproduce a flaky behavior seen in CI one can execute that script locally to run the tests
in the same order.

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

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

    $ pip install pytest-replay

Or with conda::

    $ conda install -c conda-forge pytest-replay


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

Contributions are very welcome.

Tests can be run with `tox`_ if you are using a native Python installation.

To run tests with `conda <https://conda.io/docs/>`_, first create a virtual environment and execute tests from there
(conda with Python 3.5+ in the root environment)::

    $ python -m venv .env
    $ .env\scripts\activate
    $ pip install -e . pytest-xdist
    $ pytest tests


Releases
~~~~~~~~

Follow these steps to make a new release:

1. Create a new branch ``release-X.Y.Z`` from ``master``;
2. Update ``CHANGELOG.rst``;
3. Open a PR;
4. After it is **green** and **approved**, push a new tag in the format ``X.Y.Z``;

Travis will deploy to PyPI automatically.

Afterwards, update the recipe in `conda-forge/pytest-replay-feedstock <https://github.com/conda-forge/pytest-replay-feedstock>`_.


License
-------

Distributed under the terms of the `MIT`_ license.


Issues
------

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

.. _`Cookiecutter`: https://github.com/audreyr/cookiecutter
.. _`@hackebrot`: https://github.com/hackebrot
.. _`MIT`: http://opensource.org/licenses/MIT
.. _`BSD-3`: http://opensource.org/licenses/BSD-3-Clause
.. _`GNU GPL v3.0`: http://www.gnu.org/licenses/gpl-3.0.txt
.. _`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/nicoddemus/pytest-replay/issues
.. _`pytest`: https://github.com/pytest-dev/pytest
.. _`tox`: https://tox.readthedocs.io/en/latest/
.. _`pip`: https://pypi.python.org/pypi/pip/
.. _`PyPI`: https://pypi.python.org/pypi
