Testing in Python

Mohammed Zalihat oyarazi
3 min readJul 17, 2020

Imagine you have spent hours writing codes, and it is time for your client or boss to test and it gives it one very ugly error, you are standing there wondering if you are God’s step child, because you have tested it and it worked.

That’s where testing comes in, testing is like your best friend that stops you from looking stupid in front of people.

So how do you write test code in python?

Pytest is a testing frame work which allows us to write test codes using python. It has very simple and easy syntax, it is open source, and can run a specific test or a subset of tests.

Step 1: Installation.

This post assumes you are already familiar with python.

To install pytest we will use pip command,

pip install pytest

To confirm your installation,

py.test –h

Step 2: How pytest identifies the test file and test methods.

By default, pytest only identifies the file/ method name starting with test_ or ending with _test.

Here are examples of valid and invalid pytest file names

1. test_square.py — valid

2. square_test.py — valid

3. testsquare.py — invalid

Here are examples of valid and invalid pytest method names

1. test_square() -Valid

2. square() — Invalid

Step 3: writing the test code

We will be writing a test code for a square function, the function takes a number and returns the square of the number

def square(x):“””Returns the square of a numberargs:x (int,float): numberreturns:int, float: the square of x“””return x * x

Now we write the test code for the function

#import pytestimport pytest# import the functionfrom Square import squaredef test_square():assert square(2) == 4, “Passed”assert square(2.0) == 4.0, “Passed”assert square(5) == 25, “Passed”assert square(5.0) == 25.0, “Passed”assert square(0) == 0, “Passed”assert square(-1.0) == 1.0, “Passed”

what the above code does is to test the function with the possible inputs and also assert that the function returns the correct value. In this case we test the code with a positive number, a negative number and a zero.

Step 4: running the test

To run this test, traverse to the directory where the test file is saved and run command

py.test

How do you know if your code passed or failed the test.

If you do not get any error message after you run the test that means your test passed. conventionally, a dot after the test file name shows that your code passed the test, just like the one above. If you get an error message and an F in front of the filename that means your code failed the test. If your code fails a test all you need to do is to check the error message and apply the changes to the code and test again till it passes. Here is an output of a failed test.

The reason why the code failed the test is because i changed the code to this

def square(x):“””Returns the square of a numberargs:x (int, float): numberreturns:int, float: the square of x“””return x + x

So the error is saying the code is returning 10 instead of 25 when giving the number 5, to fix this this you just need to change the + to * and the code will pass the test.

You can find the code here

THANK YOU

--

--