Frequently Asked Question
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
Hello and welcome to our first lesson on Pytest.
Here we will be showing how to write yourfirst test using this Python test framework.
In this module, we will be using PyCharm towrite and execute tests.
A guide for how to set up your PyCharm environmentis available in the Lesson Materials.
To get started, create a new project at thedesired location and select the interpreter.
Let’s create our first test file by right-clickingon the folder’s name, selecting “new”
and “Python file”.
Pytest works by searching for files that startwith “test_” or end with “_test”,
for example, “test_file1” or “file1_test”.
It is a common practice to go with the firstoption.
So, let’s create our first test file withthe name “test_file1”.
We will be writing our tests inside this file.
Pytest will search inside files starting with“test_” for functions that also start
with “test_”.
Let’s create a test to check if a numberis greater than 2.
For now, we can define the variable “x”equals 3 as our test subject.
The passing or failing of a test depends onan assertion being True or False.
In Pytest, we use the “assert” keywordfollowed by the statement we expect to be
True, so the test passes.
In this case, “assert x > 2”.
In order to run this test, we will use thePyCharm terminal.
The PyCharm terminal behaves the same as ifyou had opened the Windows Command Prompt
at the project folder location and run thetest from there.
Now, you just type “pytest” and it willautomatically search and execute all the tests
in the project folder.
After running the command, you can observethat we have only one test, and it has passed.
You can also observe that the report containsa green dot for each test that passed.
We can also add more tests in the same file.
In this second test let’s check if x isequal to 3.
Before running the test again, it is possibleto clear the command prompt either by right-clicking
and selecting “Clear Buffer” or using“CTRL+L”.
After clearing the prompt let’s executePytest again.
After running Pytest, you can observe thatwe have two tests they are both passing.
You can also observe that the report containstwo green dots.
We can also add a test that will fail to checkhow it looks like.
In this third test let’s check if x is greaterthan 5.
Let’s clear the command prompt with CTRL+Land run the test with Pytest.
After running Pytest, you can observe thatnow we have one test failing and two passing.
You can also observe that the report containsmore details when it fails.
So, the first two tests have only the green“.” showing that they passed and the third
one has the red “F” showing it failed.
The report also shows a snippet of code exactlywhere it failed and under what condition.
Your test might fail because it has errors,so let’s create a test with an error to
check how it looks like.
In this fourth test let’s add a divisionby zero.
This will also be reported as a fail in yourtest execution.
After running Pytest, you can observe thatnow we have two failed tests and two passing
tests.
The detailed report shows that the first onefailed as expected and the second one returns
a “zero division error” instead of anassertion error.
We can also have several files and executethem at the same time.
So, let’s duplicate “test_file1.py”as “test_file2.py”, clear the command
prompt and run the test.
After running Pytest, you can observe thatwe doubled the number of tests, with 4 tests
failing and 4 tests passing.
The report also shows the exact location andfile where the tests are failing.
We can also place these tests in differentfolders.
Let’s create a folder by right-clickingon the project folder, selecting “new”
and “python package”.
The folder’s name does not need to startwith “test_” so you can name it “folderA”,
for example.
It is important to mention that if you aregoing to use the same file name in different
folders you need to create these folders asmodules.
In other to do that, it is necessary to havean “__init__.py” file inside the folder,
which can be an empty file.
Further, let’s copy and paste these testfiles from the root inside folderA.
Finally, let’s copy and paste folderA witha different name: folder and run Pytest again.
We successfully managed to run several testssequentially, with 12 tests passing, 12 tests
failing.
In this lesson we learned how to write, executeand evaluate several test results.
In the next lesson, you will learn how toselect specific tests to run.