Test Driven Development with Amazon Q Developer
Accelerate through the TDD cycle with Amazon Q Developer
Mohammed Fazalullah
Amazon Employee
Published May 2, 2024
Test-Driven Development (TDD) is a software development process that relies on the repetition of a very short development cycle: first, the developer writes a failing test case that defines a desired improvement or new function. Then, the minimum amount of code is written to pass that test, and finally, the code is refactored to meet acceptable standards.
By writing tests first, developers can focus on requirements before writing the code, thus minimizing the risk of feature creep and ensuring that the code does exactly what it's supposed to do.
- Benefits:
- Improved code quality and reduced bug rate.
- Clearer understanding of the product requirements.
- Simplified debugging and maintenance.
- Challenges:
- Initial learning curve and potentially slower development pace.
- Requirement of writing effective test cases.
- Overhead of maintaining test suites as the application scales.
While TDD may require an initial investment of time and effort, it can pay off significantly in the long run by producing more robust, maintainable, and extensible code. Many successful software projects, including those at companies like Amazon, have adopted TDD as a standard practice.
Amazon Q Developer is a generative AI-powered tool designed to assist developers throughout the software development lifecycle, including during test-driven development (TDD).
Here are some ways Amazon Q Developer can help you with TDD:
- Generating Test Cases: You can ask Amazon Q Developer to generate test cases for a specific function or module based on the requirements or specifications you provide. This can help you quickly create a starting point for your tests, saving time and effort.
- Explaining Test Cases: If you're unsure about how to write a particular test case or what edge cases to consider, you can ask Amazon Q Developer to explain the test case and provide guidance on how to implement it.
- Refactoring Tests: As your code evolves, you may need to refactor your tests to ensure they remain relevant and effective. Amazon Q Developer can assist you in refactoring your test cases, suggesting improvements or alternative approaches.
- Debugging Test Failures: When a test fails, Amazon Q Developer can help you understand the root cause of the failure by analyzing the code and test case, and providing suggestions for resolving the issue.
- Answering Questions: If you have questions about TDD best practices, testing frameworks, or any other testing-related topics, you can ask Amazon Q Developer for guidance and explanations.
To use Amazon Q Developer for TDD, you can integrate it into your integrated development environment (IDE). Follow the instructions mentioned in the blog posts below to install and configure Amazon Q Developer in your favourite IDEs:
It's important to note that while Amazon Q Developer can be a valuable tool for TDD, it should be used in conjunction with your own expertise and judgment. The generated code and suggestions should be reviewed and validated to ensure they meet your project's requirements and follow best practices.
The rest of the blog post will focus on the TDD approach in Python and can be extended to other languages currently supported (Java, C#, TypeScript) by modifying the relevant prompts.
- Red: Write a test that fails because the intended functionality isn't implemented yet.
- Green: Write minimal code necessary to pass the test.
- Refactor: Clean up the new code, ensuring it fits well with the existing architecture and maintains readability and maintainability.
- Example:
- Red: Write a test for a function that calculates the sum of two numbers.
- Green: Implement the function to simply return the sum.
- Refactor: Optimize the function, perhaps by ensuring it handles edge cases like large numbers or non-integer inputs.
What does this look like in a new Python project? Let's find out!
We'll use a new Python project that has the below folder structure and files.
As you can see in the image above, the empty class file and test file are kept open side by side to help provide context to Amazon Q Developer.
To start with a test case, the
unittest
and TaskList
modules are imported into the test file along with naming the test class TaskListTest
. This helps Amazon Q Developer get some context and start suggesting relevant code to complete the test case.After generating the first test, the test case is executed in the terminal. As expected, the test case fails. Congratulations!
Now let's write code to pass this test case by switching to the
task_list.py
that will contain the application logic and leverage Amazon Q Developer to suggest relevant code. Start by providing the class name TaskList
and a docstring describing the class to provide Amazon Q Developer more context, which is then used to generate the relevant functions.After tweaking the test case to reflect the class initializer and running the test, the first test case passes!
Let's do the next round and work on test case to add a new task. Here the fields in a task will also be suggested by Amazon Developer Q after some manual intervention by adding a task ID. This changes our expectation of the fields a Task will take now which is: ID, Name and Description.
Running this test will fail, which means the
TaskList
class will be modified next as shown below. Re-running the test shows a different error now because of how the task is created as a dictionary and appended into the task
array.This is where we can leverage the chat assistant part of Amazon Q Developer. Highlighting the line of code that throws the error and right-click and selecting
Send to Amazon Q -> Send to Prompt
, and in the chat window prompting with I am getting the following error for the below line of code. "AttributeError: 'dict' object has no attribute 'id'". Please suggest a fix.
, Amazon Q Developer suggests the below shown changes.Updating the test file with the recommended change, we then run the test and see it pass successfully.
After continuing through the cycle of Red-Green-Refactor with Amazon Q Developer assisting in writing test cases and the business logic, we get 4 test cases passing successfully for testing CRUD operations on the
TaskList
, with the final code as shown below.Check out the Amazon Q on AWS Developer Center and documentation. Engage with AWS Community posts here. Also don’t forget to follow AWS Developers social profiles - YouTube, Twitter/X, LinkedIn, Instagram, and Twitch —to stay updated with the latest news and content!
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.