Choose a local coding model for Python unit tests by checking whether its tests pass a correct implementation and fail when you deliberately change a required behavior. On a Mac, give each model the same small function specification, keep the reference and altered copies outside its prompt, and compare the tests before accepting any into your project.
This exercise uses invented shipping tiers and Python’s standard library. You need Python 3 and a local app with at least one downloaded model that accepts text prompts. You can compare a coding model such as Qwen2.5-Coder 7B with a general instruction model you already use. Record the exact local build of each model. The small function makes it possible to tell whether the tests cover the requirements as well as whether they run. Qwen2.5-Coder-7B-Instruct model card · Qwen2.5-Coder 7B in Ollama · Python’s unittest documentation.
Give the models a contract to test
Start with a function that returns a predictable result from short inputs. Units, inclusive boundaries, and a rule that takes precedence give a test writer several useful cases. A test suite can cover those cases without knowing how the function is written. The generated tests should use the public function the same way a real caller uses it.
Make a fresh working folder for this exercise. Use separate subfolders for each candidate, with the same two filenames in each. The implementation will be packing.py, and the generated tests will be test_packing.py. Save the reference below in a safe place where you can copy it back. For a fair trial, send each model only the contract in the next section. Keep the reference and the intentional changes for your own review.
def shipping_tier(grams, pickup):
if pickup:
return "pickup"
if grams <= 500:
return "letter"
if grams <= 2000:
return "parcel"
return "freight"
This reference accepts a nonnegative integer weight in grams and a Boolean pickup flag. The exercise leaves negatives, decimal weights, and other types outside the contract. If a generated test insists that an invalid input must raise an exception, review that assertion against the contract before treating it as a model advantage. The exercise is about the behavior you have specified and can independently confirm.
Ask for tests in one fresh chat per model
Open a new local chat, select your candidate, and use the same prompt. Keep it clear which module will be imported and what files you want back. Your test harness can then run each output with the same command.
Write one Python unittest file named test_packing.py.
Import shipping_tier from packing.
The function accepts grams as a nonnegative integer and pickup
as a Boolean. Return values are strings:
- Pickup is always "pickup", at any permitted weight.
- For delivery, 0 through 500 grams is "letter".
- For delivery, 501 through 2000 grams is "parcel".
- For delivery, 2001 grams and above is "freight".
Test this contract using only Python's standard library.
Use the imported function and return only the test file.
Save the response for each candidate. Copy the code into that candidate’s test file, then inspect the imports and test methods. The tests should call the imported function and compare its result to an expected value. Check that the file contains only the local test code needed for this exercise. If it defines another shipping function and tests that function instead, record the problem and give the next candidate the original prompt.
Keep the model environment comparable for this particular request. A new chat with one contract gives both candidates the same task. An editor integration can add open files or coding instructions behind the scenes. If you use one, record what it supplied and run both models in the same interface with the same file selection. Check the result in Terminal with the same Python executable for every candidate. That makes an import or test discovery difference easier to trace to a file.
Put the reference function into packing.py next to the generated test file. From that working folder, run the exact test module.
python3 -m unittest -v test_packing
Read the reported test names and count. A run with zero discovered tests gives you no information about the contract. A syntax or import error tells you the suite did not run as supplied. If any assertion fails on the reference, inspect its input and expected return against the rule that applies. A failing reference test can reveal something wrong with the test, the copied file, or the expectation. Record which before editing anything or comparing candidates.
Check which intentional changes the tests catch
After a candidate’s suite passes the reference, replace packing.py in its scratch folder with a copy that has one change. Leave test_packing.py exactly as generated. Start with the lower boundary. Change the first delivery comparison from less than or equal to 500 to strictly less than 500. In this variant, a delivery weighing exactly 500 grams incorrectly becomes a parcel. A test at 499 or 501 alone can still pass.
Run the same test command again. Read the actual assertion if the suite fails. You want to see that a call at 500 grams expected a letter while the altered function returned a parcel. An import error or a test failure about an unrelated condition provides a different signal. Record the exact case that detected the change. Then restore the known-good implementation and confirm the suite passes again.
For the second variant, start from another fresh copy of the reference. Move the pickup check below both delivery thresholds, keeping it above the final freight return. A pickup weighing 120 grams now incorrectly returns letter. A pickup weighing 2100 grams continues to return pickup in that variant. Check both behaviors when deciding whether the suite exercises the priority rule. The useful failure is a pickup test at a weight where the altered order changes the actual answer.
For a third independent variant, restore the reference, then make the second delivery comparison strictly less than 2000. This changes the result at 2000 grams from parcel to freight. Keep the exact example values to yourself until scoring both model outputs. If one suite tests the lower boundary but misses the upper boundary, record both outcomes separately. The two tests answer different coverage questions even though both are caused by a comparison operator.
Use a fresh test process each time. If the test result looks like it used a previous copy, check the terminal folder and the path of the imported module. You can print the imported file path from the working folder with the same Python executable. Complete one candidate folder at a time and restore its original reference after the last run. This keeps the generated test file unchanged while the implementation changes.
For another control, make one version that only adds a comment or renames a local variable while keeping the public call and return values the same. The generated suite should still pass that version. If it expects the source of the function to contain a particular comparison or comment, the suite will break when someone refactors it. Record this separately from missing a behavioral change. A suite can detect every altered result in the exercise and still be inconvenient to maintain if it also fails on harmless edits.
Compare useful coverage and review effort
A compact record can use one row per model. Write down the exact model tag, the reference test result, whether each altered function produced the expected assertion failure, and anything you had to fix manually. Compare the cases that passed and failed before counting the number of test methods. Several methods can repeat the same ordinary weight. A short suite that uses both edges and exercises pickup at a conflicting weight can give you more coverage for this contract.
Keep the first attempt separate from a repaired attempt. If you tell one model that it missed the upper boundary and ask it to revise, its second result shows how it responds to feedback. It is no longer the same request you sent the other model. When you want to measure revision behavior, give both candidates their own test result and the same opportunity to repair it. Preserve the first and second files so the differences remain inspectable.
Read every proposed test you plan to keep. A useful unit test tells you which behavior broke and uses inputs a teammate can recognize. The smallest cases are often easiest to maintain. If the suite relies on private details, such as the function’s source code or the order of its internal lines, make the request narrower and retest it with the unchanged public contract. Changes in implementation should be able to preserve a correct result.
Look at unattended or skipped cases as well. A name such as test_upper_limit sounds relevant, but the case contributes only when it runs and checks the value at the upper limit. Read the assertion and the reported status together. If a model prints a result without asserting it, note the missing check and calculate the time needed to repair it before keeping the suite.
Track how long the complete review takes if time is part of your decision. Generating code is one step. Copying it into the right folder, running the suite, understanding the failures, and cleaning up unclear test names also take time. If two models catch the same errors, keep the one whose first output takes less work to review or integrate. If one model catches a distinct boundary that matters to your work, check that pattern on another function before giving it a broader role.
Use the result for your next real test task
Choose a representative function from your own project when you are ready, with expected behavior you can check independently. Keep its existing tests and add a new generated suite to a separate working copy. Review the selected cases and run the normal project checks after accepting the relevant tests. Repeat the simple shipping exercise after a model or runtime update if you need to confirm the new build behaves similarly.
An individual benchmark can help you shortlist models on a compatible Mac. The small-code-edit benchmark below covers a different task, where the model supplies a change to the implementation. Here, the model supplies tests that must detect changes while passing the reference. Keep those results separate when choosing which model to use for each kind of work. See a published small-code-fix benchmark for Qwen2.5-Coder 7B · Choose a local AI model for your Mac.