Unit testing in Python

Photo by Markus Spiske on Unsplash

The below installation is for Mac OS.Generally Python comes pre-installed with Mac

Install Homebrew. Homebrew is a package manager for Mac OS.

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then install python using Homebrew.

brew install python

Using Homebrew to install python would also add the python path.

To check if you have python

python — version

To check if you have pip

pip — version

To install pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Or if you don’t have curl then

python get-pip.py

You may need Xcode to run your python especially if you are using node.

Xcode is Apple’s Integrated Development Environment (IDE) or you can use CLI

Install Xcode or the alternative is installing Command Line Tools in Mac (less in size compared to Xcode)

How to Install Command Line Tools in Mac OS X (Without Xcode) you got go to this site

https://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/

If it still gives errors try this

//taken from stack overflow

xcode-select — print-path

# in my case /Library/Developer/CommandLineTools

# the next line deletes the path returned by the command above

sudo rm -rf $(xcode-select — print-path)

# install them (again) if you don’t get a default installation prompt

xcode-select — install

Or go to this site

https://stackoverflow.com/questions/9329243/how-to-install-xcode-command-line-tools

Unit testing is a software testing method which determines the quality of your code.

Generally, when the development process is complete, the developer codes criteria, or the results that are known to be potentially practical and useful, into the test script to verify a particular unit’s correctness. During test case execution, various frameworks log tests that fail any criterion and report them in a summary.

The developers are expected to write automated test scripts, which ensures that each and every section or a unit meets it’s design and behaves as expected.

The Python unit testing framework, sometimes referred to as “PyUnit,” is a Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in turn, a Java version of Kent’s Smalltalk testing framework.

The unit test framework in Python is called unittest, which comes packaged with Python.

Unit testing makes your code future proof since you anticipate the cases where your code could potentially fail or produce a bug. Though you cannot predict all of the cases, you still address most of them.

Knowing how to write assert statements in Python allows you to easily write mini-tests for your code.

Generally unit tests are written with assert statements. There are Many types of assert statements. Here I have shown only a few.

Here I have two stocks and written tests on the ratio of two stock prices and test if the ratio is less than one, equal to one or greater than one.

I have given the price of two stocks for testing the condition and the ratio = price_a/price_b .

//also import the specific function from the file on which the tests are run

//from file import functions or * for importing all

//we are testing on the getRatio function which is in another file

//Self here is the instance of the class

import unittest

class ClientTest(unittest.TestCase):

def test_getRatio_priceBZero(self):

price_a = 119.2

price_b = 0

self.assertIsNone(getRatio(price_a, price_b))

assertIsNone(expr, msg=None)

This function will take two parameters as input and return a boolean value depending upon assert condition. If input value is equal to None assertIsNone() will return true else return false.

def test_getRatio_priceAZero(self):

price_a = 0

price_b = 121.68

self.assertEqual(getRatio(price_a, price_b), 0)

assertEqual(first, second, msg=None)

Test that first and second are equal. If the values do not compare equal, the test will fail.

def test_getRatio_greaterThan1(self):

price_a = 346.48

price_b = 166.39

self.assertGreater(getRatio(price_a, price_b), 1)

assertGreater(first, second, msg=None)

This function will take three parameters as input and return a boolean value depending upon the assert condition.

This function checks that if the first given value is greater than the second value and returns true if it is so, else returns false if the first value is not greater than the second value.

def test_getRatio_LessThan1(self):

price_a = 166.39

price_b = 356.48

self.assertLess(getRatio(price_a, price_b), 1)

assertLess(first, second, msg=None)

This function will take three parameters as input and return a boolean value depending upon the assert condition.

This function checks that if the first given value is less than the second value and returns true if it is so, else returns false if the first value is not less than the second value.

def test_getRatio_exactlyOne(self):

price_a = 356.48

price_b = 356.48

self.assertEqual(getRatio(price_a, price_b), 1)

if __name__ == ‘__main__’:

unittest.main()

Resources

https://www.datacamp.com/community/tutorials/unit-testing-python

unittest — Unit testing framework — Python 3.9.2 documentation

https://docs.python.org/2.7/library/unittest.html#unittest.TestCase.assertGreaterEqual

https://www.geeksforgeeks.org/python-unittest-assertisnone-function-2/

https://www.geeksforgeeks.org/python-assertgreater-function-in-unittest/

https://www.geeksforgeeks.org/python-assertless-function-in-unittest/

Flatiron School Alumni | Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store