Test Driven Development with Amazon Q Developer
Accelerate through the TDD cycle with Amazon Q Developer
- 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.
- 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.
- 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.

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.

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.


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.
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.

TaskList
, with the final code as shown below.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.