Introduction
In today’s rapidly evolving software development landscape, the need for efficient and reliable testing solutions has never been greater. That’s where qodo comes in. It’s a AI based that helps to simplify and automate the test generation process. In this blog, we will delve into how qodo can be employed to effortlessly generate tests for an open-source PHP project. We will be looking at the Google API Client Library for PHP as our project.
qodo seamlessly integrates with Integrated Development Environments (IDEs), allowing developers to automatically generate meaningful tests. Our focus in this tutorial is on the Google API Client Library for PHP, a project with an extensive list of tests covering various Google APIs such as Gmail, Drive, YouTube and more.
PHPUnit & Testing
PHP remains a powerhouse in web development, and PHPUnit serves as a robust testing framework for PHP projects. The project that we are working with is built to provide simple access to Google APIs and is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box and a phpunit.xml file is already set up for the application by default. PHPUnit allows you to organize your tests into test suites, which are logical groups of tests. This helps you run specific sets of tests and provide better organization for your tests.
How To Test PHP based projects With qodo
There are three ways to generate tests with qodo:
- Generate Tests From Components Directly: Ideal for testing components with straightforward setups, such as business logic classes and utility code.
- Generate tests from a Single, Simple Test: Tailor tests based on an existing simple test, incorporating the project’s testing style. Some people write a single test in the “style” of the project, and then generate tests on top of them.
- Extend Existing Test Suite: Enhance the existing test suite, especially useful for complex setups or when adhering to preferred practices. While currently supporting JavaScript, TypeScript, and Python, PHP support is on the horizon.
qodo Pane – Features and Configurations
Let’s look at the configuration options before we continue. You can add instructions – which are specific prompts – that can guide qodo in test generation. We’re going to use the PHP project’s standard PHPUnit framework, and can also optionally incorporate reference tests for a more tailored approach.
Moreover, you can customize the number of tests created – more or less than the default 6.
In addition, qodo allows you to tweak existing tests automatically. For instance, if you identify a missing check, or want to check the credentials of a user before the file upload, you can just use this chatGPT-like functionality to change the existing test. Now let’s get to generating the tests.
Test Generation
Our focus today would be enhancing the test suite for REST APIS. Let’s start with Google Drive APIs. Looking more carefully at the test Suite it is evident that we have pretty good test coverage and every line is covered – so coming up with new test ideas is a pretty difficult task. This is exactly where qodo really shines. We’ll let qodo brainstorm some tests for us for testing the URL Authenticator. It delves into the core function of the API client, generating a variety of tests that can enhance the testing suite significantly.
Once the tests have finished generating, we can find a set of tests crafted by qodo tailored specifically to our project in the advanced tests pane. We can add these tests to our project and run it within the test suite.
You can also provide additional instructions for instance to add an additional clientID with an updated expected URL using the TEST GPT functionality.
Now that we have added tests from qodo’s suggestions, reviewed them, and even modified then using TestGPT, let’s now take a look at how we can generate a completely new custom test with qodo. We’ll first let the AI generate some tests for us based on the component of our choice – we are particularly focusing on the decoding of the HTTPS response given a particular stream of input. Let’s see what tests are generated by AI to test the decodeHTTp response function.
We have a test to decode the response and check for the HTTPS status code given a valid json. But what happens when an invalid JSON stream is passed? Ideally we would want to handle it graciously with proper status code. Let’s ask for a test using the New custom test widget. We will allow qodo to generate this test for me which will supposedly check if the API is able to handle an invalid JSON stream.
We can see that the test generated checks for the format. And checks this against the expected HTTP status code
It’s worthwhile to note that qodo has a PR pull request assistant which you can use to automate many of the tasks associated with creating a pull request – like generating a description, suggesting improvements, looking for similar issues and more.
Summary:
In conclusion, we’ve successfully harnessed the capabilities of qodo to generate and enhance tests for an open-source PHP project. We’ve navigated through the intricacies of testing within the Visual Studio code environment, ensuring that our tests align seamlessly with the project’s objectives. To explore further and integrate qodo into your PHP projects, visit qodo.ai. Thank you for joining us on this journey, and happy coding!