Frequently Asked Question

Video: 6.3.2 Test selection
Last Updated 3 years ago


Press "Ctrl + F" to find the keyword of your interest.

If you wish to have a direct link access to the video timestamps, please follow these instructions.

Found this video helpful? Why not take the whole HIL Specialist course? A Certificate is waiting for you for free at HIL Academy.

Would you or your organization benefit from having these videos narrated in your native language? Contact us and let us know if you wish to contribute.

TRANSCRIPT

00:00:02

Hello and welcome to our second lesson on Pytest.  

00:00:05

In this lesson, we will look at how to select a specific test to run using Python. 

00:00:10

In order to do so, we will be using some Pytest arguments. To check the list of  

00:00:15

commands you can execute pytest -h . This will return the list of arguments you can use. 

00:00:26

Let's clear the command prompt with CTRL+L and start. In the[SC1][HM2] first example,  

00:00:31

we will cover how to select a specific test by its folder name.  

00:00:34

For example, to run all the tests from folderA , you only need to type pytest  

00:00:39

folderA . Here we are also using the --collect-only -q arguments,  

00:00:43

in order to collect all the tests, and reduce the amount of text in the command line, respectively. 

00:00:54

After running the code, you can observe that only the tests inside folderA are selected.  

00:00:59

Here, the tests were just collected and not executed. If you want to run the tests,  

00:01:04

you need to remove the argument --collect-only .It is also possible to be more specific and select  

00:01:10

any desired file. For example, collect test_file1 inside folderA , with the following command: 

00:01:27

You can observe that it only collected all the tests inside test_file1 from folderA . 

00:01:32

It is also possible to specify the test itself. For example,  

00:01:36

let s run only the test_greater_than_2 , by adding it after two colons. 

00:01:50

After running the code, you can observe that only test_greater_than_2 is collected. 

00:01:55

Another way you can specify the test is by using the -k argument. This argument  

00:01:59

will filter all the tests by their function, file, and folder names. So, let's test it. 

00:02:16

Here, all tests from file1 inside folderA , folderB , and the root are collected. 

00:02:22

The same will happen for file2 . 

00:02:29

We can also do this with entire folders: 

00:02:39

We can search for functions by only including a piece of the name, like so: 

00:02:51

You can even use logical expressions for more flexibility: 

00:03:20

After running the code, you can observe that all the greater and equal tests,  

00:03:24

not containing 5 or folderA are collected. Note that the expression must be between quotes. 

00:03:31

Another option to select tests is by adding marks. For example, open file1 from the root,  

00:03:37

import Pytest, and add the Pytest mark decorator on top of the equal function,  

00:03:42

followed by your desired mark name. Let s say this is a slow test, so we can mark it as slow. 

00:03:58

In order to select tests based on marks and not names, we change the -k with -m : 

00:04:17

After running the code, you can observe that only the test marked as slow is collected.  

00:04:21

Along with the test collected in the report, there is a warning. It says that the mark created is  

00:04:26

not recognized in the list of allowed marks. This is just a warning to avoid type errors,  

00:04:31

and if you click on the provided link, it will open the Pytest documentation. This documentation  

00:04:37

shows how to add this mark to the list of registered marks so you can avoid this warning. 

00:05:02

It is possible to add the same mark to several tests. For example,  

00:05:06

the test_greater_than_5 could also be slow .  

00:05:18

We can also add several marks to the same test.  

00:05:21

So, lets also add the gt mark for the greater_than tests. 

00:05:52

Just like for the -k argument, here you can also use logical expressions: 

00:06:08

There is also a special marker name to skip tests. You can use it to skip tests you know  

00:06:13

are broken or are not implemented yet. Let s use it on our test_with_error . When running Pytest,  

00:06:19

it shows how many tests are skipped. These are represented with an s next to the test name. 

00:06:37

In this lesson we learned how to select tests. In the next one,  

00:06:41

we will explore how to parametrize tests.

00:06:44

Thank you for watching!

Please Wait!

Please wait... it will take a second!