Frequently Asked Question

Video: 6.3.1 Writing your first test
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 first lesson on Pytest.

00:00:05

Here we will be showing how to write yourfirst test using this Python test framework.

00:00:09

In this module, we will be using PyCharm towrite and execute tests.

00:00:14

A guide for how to set up your PyCharm environmentis available in the Lesson Materials.

00:00:18

To get started, create a new project at thedesired location and select the interpreter.

00:00:28

Let’s create our first test file by right-clickingon the folder’s name, selecting “new”

00:00:35

and “Python file”.

00:00:37

Pytest works by searching for files that startwith “test_” or end with “_test”,

00:00:43

for example, “test_file1” or “file1_test”.

00:00:47

It is a common practice to go with the firstoption.

00:00:50

So, let’s create our first test file withthe name “test_file1”.

00:00:57

We will be writing our tests inside this file.

00:00:59

Pytest will search inside files starting with“test_” for functions that also start

00:01:04

with “test_”.

00:01:05

Let’s create a test to check if a numberis greater than 2.

00:01:09

For now, we can define the variable “x”equals 3 as our test subject.

00:01:13

The passing or failing of a test depends onan assertion being True or False.

00:01:18

In Pytest, we use the “assert” keywordfollowed by the statement we expect to be

00:01:22

True, so the test passes.

00:01:24

In this case, “assert x > 2”.

00:01:26

In order to run this test, we will use thePyCharm terminal.

00:01:32

The PyCharm terminal behaves the same as ifyou had opened the Windows Command Prompt

00:01:36

at the project folder location and run thetest from there.

00:01:40

Now, you just type “pytest” and it willautomatically search and execute all the tests

00:01:44

in the project folder.

00:01:47

After running the command, you can observethat we have only one test, and it has passed.

00:01:53

You can also observe that the report containsa green dot for each test that passed.

00:01:57

We can also add more tests in the same file.

00:02:01

In this second test let’s check if x isequal to 3.

00:02:04

Before running the test again, it is possibleto clear the command prompt either by right-clicking

00:02:16

and selecting “Clear Buffer” or using“CTRL+L”.

00:02:19

After clearing the prompt let’s executePytest again.

00:02:25

After running Pytest, you can observe thatwe have two tests they are both passing.

00:02:30

You can also observe that the report containstwo green dots.

00:02:34

We can also add a test that will fail to checkhow it looks like.

00:02:37

In this third test let’s check if x is greaterthan 5.

00:02:42

Let’s clear the command prompt with CTRL+Land run the test with Pytest.

00:02:54

After running Pytest, you can observe thatnow we have one test failing and two passing.

00:02:59

You can also observe that the report containsmore details when it fails.

00:03:03

So, the first two tests have only the green“.” showing that they passed and the third

00:03:08

one has the red “F” showing it failed.

00:03:10

The report also shows a snippet of code exactlywhere it failed and under what condition.

00:03:16

Your test might fail because it has errors,so let’s create a test with an error to

00:03:21

check how it looks like.

00:03:22

In this fourth test let’s add a divisionby zero.

00:03:25

This will also be reported as a fail in yourtest execution.

00:03:41

After running Pytest, you can observe thatnow we have two failed tests and two passing

00:03:48

tests.

00:03:49

The detailed report shows that the first onefailed as expected and the second one returns

00:03:52

a “zero division error” instead of anassertion error.

00:04:01

We can also have several files and executethem at the same time.

00:04:05

So, let’s duplicate “test_file1.py”as “test_file2.py”, clear the command

00:04:10

prompt and run the test.

00:04:18

After running Pytest, you can observe thatwe doubled the number of tests, with 4 tests

00:04:25

failing and 4 tests passing.

00:04:27

The report also shows the exact location andfile where the tests are failing.

00:04:34

We can also place these tests in differentfolders.

00:04:40

Let’s create a folder by right-clickingon the project folder, selecting “new”

00:04:45

and “python package”.

00:04:46

The folder’s name does not need to startwith “test_” so you can name it “folderA”,

00:04:51

for example.

00:04:52

It is important to mention that if you aregoing to use the same file name in different

00:04:57

folders you need to create these folders asmodules.

00:05:00

In other to do that, it is necessary to havean “__init__.py” file inside the folder,

00:05:05

which can be an empty file.

00:05:07

Further, let’s copy and paste these testfiles from the root inside folderA.

00:05:12

Finally, let’s copy and paste folderA witha different name: folder and run Pytest again.

00:05:29

We successfully managed to run several testssequentially, with 12 tests passing, 12 tests

00:05:42

failing.

00:05:44

In this lesson we learned how to write, executeand evaluate several test results.

00:05:53

In the next lesson, you will learn how toselect specific tests to run.

Please Wait!

Please wait... it will take a second!