pytest mark order not working

Viewed 6968

I have about 10 tests in same file and each one them has following set to execute in order

import pytest
@pytest.mark.order1
.
.
.
@pytest.mark.order10

But the tests never run in the order they are assigned. They always run in the order they are arranged. Anything I am missing ?

even @pytest.mark.tryfirst didn't work. One thing I noticed is, the @pytest.mark.order never shows up in suggestions while atleast @pyetst.mark.tryfirst was there in pycharm.

5 Answers

It looks like you're using pytest-ordering. That package is indeed "alpha quality" -- I wrote it and I haven't spent much time keeping it updated.

Instead of decorating with @pytest.mark.order1, try decorating with @pytest.mark.run(order=1). I believe the Read the Docs documentation is out of date.

I faced the same issue, you can use this command to order it:

@pytest.mark.run(order=1)

But before, install it from this site.
Then it will work fine.

The pytest marks do not do anything special, except for marking the tests. The marks can be used only for the purpose of filtering them with -m CLI option.

This is all pytest alone can do with the marks. Well, and few little things like parameterization & skipif's.

Specifically, there is no such special mark as tryfirst. It is a parameter to the hook declaration, but this is not applicable for the tests/marks.

Some external or internal plugins can add special behavior which is dependent on the marks.

Pytest executes the tests in the order they were found (collected). In some cases, pytest can reorder (regroup) the tests for better fixture usage. At least, this is declared; not sure if actually done.

The tests are assumed to be completely independent by design. If your tests depend on each another, e.g. use the state of the system under test from the previous test-cases, you have a problem with the test design. That state should be somehow converted to the fixture(s).

If you still want to force some dependencies or order of the tests (contrary to the test design principles), you have to install a plugin for the test ordering based on marks, e.g., http://pytest-ordering.readthedocs.io/en/develop/, and mark the tests according to its supported mark names.

It looks like you're using pytest-ordering. Make sure you already have installed pytest-ordering in you env, here de docs: https://github.com/ftobia/pytest-ordering and try to use the following decoration

@pytest.mark.run(order=1)

I was also facing the same issue multiple times and tried everything available online. However, what worked for me is to install ordering using pip3 install pytest-ordering and restart pycharm.

Related