Skip to content
Notifications
Clear all

Showcase: Using Aider to generate and run unit tests for a legacy module.

1 Posts
1 Users
0 Reactions
18 Views
(@martech_ops_mike)
Trusted Member
Joined: 5 months ago
Posts: 40
Topic starter   [#5970]

Just inherited a legacy Django module for our email preference center. The logic was... let's say "organic." No tests, of course. I've been meaning to write unit tests for weeks, but it's such a slog. Decided to see if Aider could not only write the tests but actually help me run them and iterate. Spoiler: it was surprisingly effective.

I started by just feeding Aider the main `models.py` and `views.py` files. My initial prompt was: "Generate a set of pytest unit tests for these files. Focus on testing the `update_preferences` view and the `UserPreference` model methods." What it gave me was a solid foundation—it correctly set up fixtures, used the Django test client, and hit the main CRUD operations. But the real win was the workflow:

* **First pass:** It generated about 80% of what I needed. The tests ran, but some failed due to a quirky, legacy validation function it hadn't seen.
* **Iteration:** I just told Aider the error output: "The test `test_update_preferences_invalid_data` is failing because of a `ValidationError` from a function called `validate_marketing_flags`. Here's that function from `utils.py`." I pasted the helper code.
* **Aider's move:** It updated the test to mock that function appropriately, and the suite passed.

What I loved was how it handled the back-and-forth. Instead of me writing the mock, explaining the side effect, and updating the test, I just showed Aider the error and the new source file. It connected the dots. It also suggested and added a couple of edge-case tests I hadn't considered (like testing for inactive users).

The final step was asking it to "add a command to run these specific tests in the terminal." It gave me the exact `pytest` path command, which I could copy-paste. For a module that felt like a testing desert, I now have a green, 25-test suite in under an hour. The key was treating it like a pair programmer—giving it the errors and the new context so it could course-correct.

Has anyone else used Aider specifically for adding tests to old code? I'm curious if your experience matches mine, especially around the iteration loop with failing tests.

stay automated


stay automated


   
Quote