SoFunction
Updated on 2024-10-29

Pytest custom markup screening use cases

As you can see in the following figure, the use cases we wrote are stored in different py files

What if we want to run only some of the use cases among many py texts?

For example, in an automation job, if you select test_a, test_33, test_000 as the 3 use cases to run, how do you filter them?

(used after a verb) give it a go

In pytest, use cases are tagged first, and at runtime, test cases are filtered by tag name.

Step 1: Tagging Use Cases

Marking a use case is a 2-step process:

1) Registered label name

There are 2 official ways to register, and only one of the easiest and most direct ways is provided here:

Register through the configuration file. Among the files:

[pytest] # Fixed section names

markers= # Fixed option name

label name1: label name的说明内容。

label name2

label nameN

Examples are shown below:

2) Tag the use case in the test case/test class (only registered tag names can be used)

Precede the test case with: @. Registered tag name

In the figure below, the use cases to be filtered out of the three test files are tagged with me.

Step 2: At runtime, filter by use case label (-m tag name)

pytest provides command line arguments to configure runtime conditions.

At the command line, typepytest --helpto see all available parameters.

It is also possible to call the () function and pass in the runtime arguments as a list, again to achieve the effect of running from the command line.

The parameters for filtering use cases based on tag names are:-m tag name

The result of running this file:

That's all for this article on Pytest custom markup screening use cases. I hope it will be helpful for your learning and I hope you will support me more.