DEV Community

Ajo George
Ajo George

Posted on

Testing DocBot with unittest

So as we wind up week 9, we had lab 07 which handled testing of our code, For me i always feel testing and writing test cases was a bit confusing for me, As for this lab it was even more which including mocking, for making it successfully i need a lot of references and online help !.

What i did !

As DocBot is written with python, i made use of unittest as it was the only one which i have seen and used a bit in the past.

First Task

The first task for us to write a simple test case for tool, so i decided to choose from the main file itself which is DocBot.py. Well for unittest to work we need to follow some file naming and structure. So all the files were stored under a new folder called testes.

For our first set of cases for the main file itself it was stored in test_docbot.py. I wrote very 2 simple test classes for this file.

Test for showing tool version

In this test it check if the -v is working as expected or not. I used io from StringIO to capture the output send to the console. In our case when the version flag is used we should get "DocBot", "0.3". To check this we use assertEqual which is check if the value tested is equal to value expected.

Test for checking if there is no files

In this test we are checking if the tool will give you a output saying "error: the following arguments are required: file". If there is no file given for processing. I managed this by using @patch which is a mock object helps to access the system output, errors etc in the local test case. Finally this test case looks, if exit's correctly and shows the error message to the users.

Second Task

The second task was also to write test cases, but in this case we were actually creating a mock version for our LLM, in my case Gorq API. This technique was first time for me. It tool lot of referencing and help online to finish this work.

Mocking is done in this case because we cant pass API keys and other associated attributes of an LLM for testing.

I have created 2 test classes for this file too, file is called test_api.py.

Test to check if README is generated or not

The final goal of this test was to create a mock version of the Gorq API and test if it creates the README file when files are passed to it.
We make use of MagicMock() which is available with in unittest.mock library. Basically MagicMock helps us to retrieve and hold complex values in a function. In this it is the API generation and values associated with its call.
To pass this test, when the mock API takes a file to generate the README it should have a print of README FILE (Model: llama3-8b-8192) and README content generated in the console. If yes we pass the test.

Test to check if API stopes if user didn't specify any files.

This is almost similar to the case in main file, the only difference is it is testing the main file arguments and here we are passing an non existing file to generate_readme functions, which tells there is no file to the API and checks how the API behaves.
In this case we will pass the check if the prints "Error: Source file nonexistent_file.py not found." to the users. This means there is no file called nonexistent_file.py found to be processed.

Well i know this not that perfect but, this what i made with my understanding.

Summary

As this task was complicated for me and found a bit of difficulty in completing it taught me and forced me to explore and learn new things about testing, As testing is a big part of Software Development. I still need more time to learn more about new things and testing hopefully i can improve in the future !

AJO GEORGE 08-11-2024

Top comments (0)