Pytest for Absolute Beginners

Essential tips and tricks for beginners

Chowa Cross
Analytics Vidhya

--

Pytest for beginners

Python is a dynamically-typed language used in software development and other areas such as data analysis, research science etc which has been on the increase over the past years and the need to properly, effectively and efficiently put the software’s used in these fields to the test is prominent to ensure they produce the correct expected results. Here comes “pytest”

What is pytest:

Pytest is a framework that makes building simple and scalable tests easy.

Why pytest

  • No boiler code required
  • Complex test are made simple to write
  • Tests are easy to read
  • You can get started in seconds
  • You use assert to fail a test unlike Unittest which uses things such as self.assertEqual etc
  • It can be use to run tests written for Unittest

Pytest Installation

Run the following command on your command line

pip install pytest

Conditions For Carrying out Test In pytest

In order to carry out test in pytest, the following conditions are to be made,

  • The file has to start with test_<something> or end with <something>_test
  • Tests classes should be name Test<something>

Getting started with Pytest

let’s create a simple test function in a file called test_medium.py

def func(x):
return x + 1


def test_correct():
assert func(4) == 5
def test_false_answer():
assert func (3) == 8

Next:

we run

pytest test_sample.py in our current working directory

our output

Interpretation of pytest Output

From the above output, we can see 1 test was failed and the other passed.Here is the beauty of pytest when compared to other testing frameworks i.e the test is very easy to read and understand

Assertions About Expected Exceptions

In order to write assertions about raised exceptions, you can use pytest.raises as a context manager. For example,

import pytest def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0

output

passed asserting test

Assertion Output Interpretation

From the above output, we can we passed test.

N.B:

The green dot (.) is an indication that we have ran just a single test function

Possible Outcomes of A Test function

PASSED(.):The test ran successfully

FAILED(F):The test was unsuccessful

SKIPPED(s):The test was skipped

xfail (x): The test was not supposed to pass, ran, and failed

XPASS (X): The test was not supposed to pass, ran, and passed

ERROR (E): An exception happened outside of the test function in either a fixture or hook function

How to Run a Single Test

By default, pytest looks for test in the current working directory and then sub working directories by looking for the necessary test files.

To run a single test, we specify the file directory and add a “::test_name”

Using Options

To view a list of all the available options in pytest run the following command on your command line

pytest — help

We will discuss some of the commonly used ones,

--v,-verbose : This option reports more information about our test than without

--q,-quite : This is the opposite of verbose

version : This option shows the version of pytest

--h : Shows you how to use pytest

--l,-showlocals: displays local variables and their values with tracebacks for failing tests

--lf,-last-failed: This provides us with a convenient way of writing just the failing tests

--ff ,-failed-first :Does same as -lf the run the rest of the test that pass the last time

Conclusion

The above is barely a tip of the the iceberg when it comes to testing with pytest but i do believe these provides the basic know-how for any beginner to get started with the pytest framework.Be sure to check out the references below for more information while looking forward to my intermediate tutorial as i will be covering more advance and a bit complex topics.

Meanwhile in the main time, be sure to leave a like,clap ,share,follow and comment.

Aside

Also check out my previous Tutorial on Building a Bookstore web app API with flask

References/Credits

Python Testing with Pytest by Katharine Dvorak

--

--

Chowa Cross
Analytics Vidhya

i just go by @chowacross on all social media platforms