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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 33: Line 33:
== Project Description ==
== Project Description ==


This project appears to be a Python one that uses the requests library to perform API calls to the GitHub GraphQL API, based on the instructions that were supplied. 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.
This is a Python project one that uses the requests library to perform API calls to the GitHub GraphQL API, based on the instructions that were supplied. 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 task is to refactor the existing code present in the test_suit.py and implement them in a newly forked repository.
The task is to refactor the existing code present in the test_suit.py and implement them in a newly forked repository.

Revision as of 02:34, 12 April 2023

Team

Mentor

  • Jialin Cui

Team Members

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

Description and Testing

According to the instructions, we are expected to work on a Python project that 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.

Tasks to be accomplished :

  • 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).
  • Get the code fully tested.

Files Involved

  • test_suit.py
  • requirements.txt
  • env file

Project Description

This is a Python project one that uses the requests library to perform API calls to the GitHub GraphQL API, based on the instructions that were supplied. 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 task is to refactor the existing code present in the test_suit.py and implement them in a newly forked repository.

We took the next actions to accomplish this:

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

Code Snippet :

   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.

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()

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.