CSC/ECE 517 Spring 2023 - G2334 Develop and refactor test suite for GraphQL Query

From Expertiza_Wiki
Jump to navigation Jump to search

Team

Mentor

  • Jialin Cui

Team Members

  • Aman Waoo (awaoo)
  • Girish Wangikar (gwangik)
  • Pranavi Sharma Sanganabhatla (psangan)

Problem Statement

The current Python project makes API calls to the GitHub GraphQL API using the requests package. We must rewrite the current test cases such that mocking libraries are used in place of actual API calls, and we must also add new test cases to boost the test coverage.

Need of API Mocking :

API mocking empowers developers to exercise complete control over the behavior of the mocked API, encompassing responses, errors, and delays. This level of control facilitates precise testing scenarios, such as evaluating error handling, edge cases, or desired responses from the API. Furthermore, it enables the replication of specific scenarios during testing, simplifying the identification and resolution of issues.

Project Description

Feature explanation - GitHub GraphQL API queries

GraphQL is a modern and flexible API query language that provides a more efficient and developer-friendly way to request and manipulate data from APIs compared to traditional REST APIs. This project makes use of the requests library to send requests to the GitHub GraphQL API to access user public data. We have used the pytest testing framework and the pytest-mock plugin to create tests for methods that submit requests to an API using the requests library.

Tasks and Goals to be accomplished :

We are tasked with increasing the test coverage by introducing new test cases, as well as restructuring the current test cases to use mocking libraries rather than actual API calls. The major goal is to implement the refactored existing code in the files already available in a newly cloned repository.

  • Test functions should have names that are both descriptive and explicit so that it is evident what part of the system they are testing.
  • Several tests in the current codebase make actual API calls as opposed to using mock calls. Make mock test calls from these tests.
  • Change the requirements.txt file to reflect the newly installed packages (pytest-cov and pytest-mock).
  • Update the env file for the github token.
  • Get the code fully tested.

Files Involved

  • test_suit.py
  • requirements.txt
  • env file

UML Diagram

This image illustrates the concept of mocking GitHub's GraphQL API using a GraphQL API mocking tool. (Please click on the image for a bigger view)

Work Strategy

To achieve the objectives, we conducted the following steps :

1. Use the command below to install the packages pytest-cov and pytest-mock:

Code Snippet :

   pip install pytest-cov pytest-mock

And made the following changes in the requirements.txt file to incorporate the required modules for the project :

pip install pytest-cov pytest-mock pip freeze > requirements.txt pip install pytest-cov pytest-mock

This will set up the packages your project needs for code coverage and mocking.

2. All test cases that use function B, which makes a genuine API call, have been refactored so that mocking is used in place of calling. Create a mock version of function B using the mocker.patch function offered by pytest-mock, and set the mock function's return value to the expected API response. Please refer the below code snippet for a better understanding of the statment.

Code Snippet :

  def function_b(): 
   response = requests.get('https://api.example.com/data') 
   return response.json() 
  def function_a(): 
   data = function_b() 
   # do something with the data 
   return data 
  def test_function_a(mocker): 
   mock_function_b = mocker.patch('_main_.function_b') 
   mock_function_b.return_value = {'foo': 'bar'} 
   result = function_a() 
   assert result == {'foo': 'bar'} 
   mock_function_b.assert_called_once()

We have implemented API mockingin a large number of methods and functions. Here are some code samples of the functions that used direct API calls and were adjusted by employing API mocking, as it is very difficult for us to list all the methods we have implemented:

Code snippet :

Test Plan

The pytest-cov package has been used to generate a coverage report and to pinpoint sections of the code that are not being covered by the current test cases. Next, in order to better the overall test coverage and include these areas, we introduced more test cases.

Test Execution

We divided the work among the teammates and started working on different parts of the project. To generate a coverage report, run the following command :

Code Snippet :

 pytest --cov

This will execute each test case and produce a report on the project's overall coverage. This report helped us identify parts of the code that the current test cases don't cover. We have developed new test cases to cover these regions once you have pointed them out. To imitate the intended behavior of external API calls and to account for both positive and negative eventualities, we used mocking.

Conclusion

The code in the test suit.py file has been refactored. In order to get the desired output from the methods that were using API calls, we mocked the real API method calls that were made internally. We also modified the names of all the test methods to match the functionality carried out by the methods.