PyTest Marks

PyTest allows us to use preset or create custom attributes for the test methods using PyTest markers, @pytest.mark.<name>. PyTest itself has a set of predefined marks:

  • skip to always skip a test function

  • skipif to skip a test function if a certain condition is met

  • xfail to produce an “expected failure” outcome if a certain condition is met

  • parametrize to perform multiple calls to the same test function.

Besides the predefined marks, you can also create custom marks to group your tests with various groups as Smoke, regression, Trusted, Untrusted, IOS or whatever else you need.

To create custom marks, you have to edit your pytest.ini file. Following is an example of how this file could look like:

Copy
[pytest]
# Definition of all the custom markers
markers=
    # State markers
    serial: Tests that cannot, in any way, be executed in parallel

    # Feature markers: feel free to add yours here
    # Dashboards
    user_dashboard: User Dashboard tests
    devapi: API tests
    smoke: Smoke tests
    android: android tests

The lines that start with # are comments.

The syntax is as follow:

Copy

PyTest Marks syntax

mark: description

As soon as you define the marks in this file, you can start using them in your tests.

Copy
@pytest.mark.smoke
def test_math():
    """    developer: yana.todorova
    """    old_value = 4
    new_value = 4
    assert old_value == new_value

__name__ == "__main__":pytest.main([__file__,"-k", "test_", “-v”, “-m”, “smoke”,"-s"])
Note:

This test is a simple example of how you can use marks.

You can set marks for the entire file if all tests in that file run under the same mark. Then you are setting the mark at the beginning of the file instead of above every test. You can also set a few marks to mark the file, if the included tests are part of two groups at the same time, for example an android group and a regression group.

Copy
pytestmark = [pytest.mark.android,
                pytest.mark.regressions]


def test_math():
    """    developer: yana.todorova
    """    old_value = 4
    new_value = 4
    assert old_value == new_value


def test_print():
    """    developer: yana.todorova
    """    print("it is amazing to work with PyTest")