Skip to main content

Python unit testing frameworks


Introduction:

In this post, we are going to describe the different frameworks used to run unit test for python codes. This is not a comprehensive article on unit testing in general, but the aim is to explore several frameworks for python code unit testing.  This post is intended for beginner and amateur level python developers who are interested to explore python unit testing frameworks. First lets explore the different testing scenarios one encounters.

Different testing scenarios:

There are mainly two types of testing in software, functional and non-functional. We have taken these common test types from softwaretesting  site.

Functional Testing types include:

  • Unit Testing
  • Integration Testing
  • System Testing
  • Sanity Testing
  • Smoke Testing
  • Interface Testing
  • Regression Testing
  • Beta/Acceptance Testing

Non-functional Testing types include:

  • Performance Testing
  • Load Testing
  • Stress Testing
  • Volume Testing
  • Security Testing
  • Compatibility Testing
  • Install Testing
  • Recovery Testing
  • Reliability Testing
  • Usability Testing
  • Compliance Testing
  • Localization Testing
We are going to explore unit testing for python codes in this post.

What is unit testing?

Unit testing is the first level of testing in software development life cycle or SDLC. Normally unit testing is carried out for each independent module to test their functional accuracy and logics. Therefore, it comes at the first step of the testing.
Normally unit testing for a individual component is carried out by its developer only. Developers write unit tests and then according to test results codes are often modified, upgraded or re-written.  Now that you have the idea about  what is a unit testing; we will dive into python unit testing frameworks.

Python unittesting frameworks:

Unittest module: basics and details:

First framework which we will look into is python unittest which is one of the most simple and easy to work with frameworks. Being a python framework, it supports the object oriented setup for python.
unittest provides a base class TestCase, on the top of which one builds aggregate of test cases.
The general format of unittesting class is:
import unittest
class test_you_write(TestCase):
    def SetUp(self):
        #random setup codes
    def test_goodname(self):
        #self.assertEqual(function_output, output_expected)
 
 
if __name__ == '__main__':
    unittest.main()
Import points which are not mentioned as trivial here are:
(1) it is customary to write the name of your test functions begining with "test_". if you don't obey the nomenclature, those functions will be ignored when the main function runs to test the functions.
(2) The last format is most important, as it is again a customary pattern to run tests like that.
(3) The normal pattern of unit test functions are to check whether the output of developer written function is equal to the expected output. The way we verify that is using the assertEqual function. There are a bunch of similar functions for different types of checks.

The output of this program is normally

i.e. if all the tests pass. If some of the tests fail then it will say that which test failed and at what point.

Now, lets discuss a bit more organization features of the framework. Like in normal OOP structures, some variables tend to get used in each test cases as well as some codes can get repeated. For these, it is a general notion to create those variables in a init like function inside the class first. In case of TestCase, this init like function is setUp. In setUp we can initiate those variables which we use in all the test cases. There is also a destructing pair of setUp named tearDown() which is used to close the environment variables created or instances opened during setUp(). It is important to use tearDown after completing your experiments using setUp.
An example from the unittest official documentation is as follows:
There is also a construction named TestSuite class. This works as a functional setup rather than being an OOP setting. After instantiating a object of TestSuite(); one can add tests in functions using addTest(function(parameter)) setup. Finally one can execute it again using main().

For knowing the greater details of different assert*() functions as well as features as sort of how to skip tests known for failure and others, please follow the official documentation for instructions.

Comments

Popular posts from this blog

Tinder bio generation with OpenAI GPT-3 API

Introduction: Recently I got access to OpenAI API beta. After a few simple experiments, I set on creating a simple test project. In this project, I will try to create good tinder bio for a specific person.  The abc of openai API playground: In the OpenAI API playground, you get a prompt, and then you can write instructions or specific text to trigger a response from the gpt-3 models. There are also a number of preset templates which loads a specific kind of prompt and let's you generate pre-prepared results. What are the models available? There are 4 models which are stable. These are: (1) curie (2) babbage (3) ada (4) da-vinci da-vinci is the strongest of them all and can perform all downstream tasks which other models can do. There are 2 other new models which openai introduced this year (2021) named da-vinci-instruct-beta and curie-instruct-beta. These instruction models are specifically built for taking in instructions. As OpenAI blog explains and also you will see in our

Can we write codes automatically with GPT-3?

 Introduction: OpenAI created and released the first versions of GPT-3 back in 2021 beginning. We wrote a few text generation articles that time and tested how to create tinder bio using GPT-3 . If you are interested to know more on what is GPT-3 or what is openai, how the server look, then read the tinder bio article. In this article, we will explore Code generation with OpenAI models.  It has been noted already in multiple blogs and exploration work, that GPT-3 can even solve leetcode problems. We will try to explore how good the OpenAI model can "code" and whether prompt tuning will improve or change those performances. Basic coding: We will try to see a few data structure coding performance by GPT-3. (a) Merge sort with python:  First with 200 words limit, it couldn't complete the Write sample code for merge sort in python.   def merge(arr, l, m, r):     n1 = m - l + 1     n2 = r- m       # create temp arrays     L = [0] * (n1)     R = [0] * (n

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle