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

Mastering SQL for Data Science: Top SQL Interview Questions by Experience Level

Introduction: SQL (Structured Query Language) is a cornerstone of data manipulation and querying in data science. SQL technical rounds are designed to assess a candidate’s ability to work with databases, retrieve, and manipulate data efficiently. This guide provides a comprehensive list of SQL interview questions segmented by experience level—beginner, intermediate, and experienced. For each level, you'll find key questions designed to evaluate the candidate’s proficiency in SQL and their ability to solve data-related problems. The difficulty increases as the experience level rises, and the final section will guide you on how to prepare effectively for these rounds. Beginner (0-2 Years of Experience) At this stage, candidates are expected to know the basics of SQL, common commands, and elementary data manipulation. What is SQL? Explain its importance in data science. Hint: Think about querying, relational databases, and data manipulation. What is the difference between WHERE ...

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 know...

Spacy errors and their solutions

 Introduction: There are a bunch of errors in spacy, which never makes sense until you get to the depth of it. In this post, we will analyze the attribute error E046 and why it occurs. (1) AttributeError: [E046] Can't retrieve unregistered extension attribute 'tag_name'. Did you forget to call the set_extension method? Let's first understand what the error means on superficial level. There is a tag_name extension in your code. i.e. from a doc object, probably you are calling doc._.tag_name. But spacy suggests to you that probably you forgot to call the set_extension method. So what to do from here? The problem in hand is that your extension is not created where it should have been created. Now in general this means that your pipeline is incorrect at some level.  So how should you solve it? Look into the pipeline of your spacy language object. Chances are that the pipeline component which creates the extension is not included in the pipeline. To check the pipe eleme...