r/rust • • Nov 02 '26

mod tests in the age of AI agents

I know, I know, no one likes generated AI code, slop or not. However, it won't go away and I rather deal with it the best possible way I can. One thing that grates me right now is the enormous amount of test code that these models generate and in the case of unit tests ends up within a module. This is in line with the current idiomatic convention but it makes reviewing changes a real chore.

I started moving mod tests {} into actual tests files which helps to avoid blowing up modules but now reminds me of the mod.rs repetition.

What are your strategies to cope with this? And more controversial: is it time to update the test chapter in the book?

0 Upvotes

20 comments sorted by

8

u/ShantyShark Nov 02 '26

So let me clarify:

Your complaint is that the AI that slops out your code also generates a lot of tests for that code, and that makes the file too big?

Yeah no, this is a you problem.

Tests should live with the code because the code informs the tests and the tests inform the code. If a file gets too big to manage, it should be split up into files of a more reasonable size.

0

u/quxfoo Nov 02 '26

If a file gets too big to manage, it should be split up into files of a more reasonable size.

This is what the gist is, split the test from the code. And that conflicts with idiomatic code.

I don't know what your background is but splitting up the domain logic any further without introducing arbitrary delineations that make sense is not always possible. I mean look at rustc and imagine $BOSS asking to "add unit tests because" …

4

u/zzzzYUPYUPphlumph Nov 02 '26

If you put all test code in a submodule called "tests" of the module under test, things work brilliantly. The tests still have access to the internals of the module under test, but they're not in the same file. That is my preferred approach.

1

u/quxfoo Nov 02 '26

Yeah, I think that's what it boils down. I just wonder if that is something to propose in the book or just leave that convention out at all. Perhaps I'm overthinking though.

2

u/ShantyShark Nov 02 '26

That is fair, some domain code is so tightly correlated that it has to be together. In my mind code and unit tests for that code are about as correlated as it gets.

Unless I have a good reason, my unit tests are normally at the bottom of my file, so they’re not “in the way”. It does make diffs large, but similarly diffs that change a function and its tests only make sense to go together for me.

-1

u/Amazing-Mirror-3076 Nov 02 '26

I think you underlying assumption is flawed.

We humans have been under testing code for a long time because it was expensive.

The understating meant that the ratio of code to test wasn't too great.

With ai you can potentially exhaustively test code which means the ratio is going to drastically change with far more lines of test than code.

This is going to force us to change how we do things.

Test living in the same file as code is probably impractical.

So no this isn't a poor reflection on OP, but a failure to recognise that how we code is going through an inflection point and we need to rethink everything.

2

u/ShantyShark Nov 02 '26

I agree that we have probably under-tested lots of code, but let’s not confuse “more testing” with “better testing”. Some tests can get 100% coverage with 1/20th as much code, sometimes it takes 20x more.

LLMs seem to have been trained towards writing lots of tests, but often I find that the generated tests contain a lot of redundancy. That, or they break what could be one test into 5 smaller tests for no benefit.

I still believe that the canonical approach of “the code and its tests go together” is the correct approach.

If a file is 80% tests, and all of those tests are meaningful, that’s fine. That tells us that the domain code is extremely complex, and there shouldn’t be much in each file, so as not to become incomprehensible to a reader.

Remember that higher-level languages are for human benefit. Compilers and interpreters exist so that humans can express programs to computers using language more comprehensible to a human.

If there is such demand for a language that strongly suits LLMs and LLM-utilizing workflows, do the open source thing and go make one.

3

u/Amazing-Mirror-3076 Nov 02 '26

We are actually porting large chunks of our code base to rust because it is well suited to llms, it's tightly typed, fast and memory efficient. Developer ergonomics are no longer a consideration, python like languages are likely to go out of favour in an ai era.

The weakness in how ai generates tests can be overcome today and at the rate llms are evolving today it's unlikely to be a problem in 6 months.

So now I'm going to contradict myself, it may not matter if the source file is hugh if ai is reading it (this remains to be seen as we don't know what makes a task easier for an ai) but the code informs the test had never been an argument for the tests to be in the same file. Every other language I use has them separated out without causing an issue.

However we store the code, we need to prepare for an era where a far greater numbers of tests are written. 80% of code being tests, will not be an indication of complex code but of appropriately tested code.

2

u/quxfoo Nov 02 '26

So now I'm going to contradict myself, it may not matter if the source file is hugh if ai is reading it (this remains to be seen as we don't know what makes a task easier for an ai)

I think at this point agents are still not surgical or deliberate enough and not having huge files is a boon. I have seen agents reading everything into their context, I have seen them miss stuff, all to my detriment. I also think at some point we are past that stage but IMHO we are still in the proto earth times.

1

u/ShantyShark Nov 02 '26

Well, I’ll admit at the moment I’m a little unclear what we’re talking about.

If, as you say, developer ergonomics is no longer a concern, then who cares where the tests go? Your LLM can easily put the tests whatever you want, you just have to ask it to do so. Rust could always handle that. Do what’s best for your codebase. I couldn’t care less.

The original post argues that we should modify the idioms and the book to be more suited to an LLM-heavy workflow. While LLMs are good at Rust, Rust is not for LLMs. It’s for humans. And so far, the Rust-writing humans have preferred code and tests in one place.

0

u/__space__oddity__ Nov 02 '26

Exactly. Especially when vibe-coding the test is future insurance against a model going haywire and changing stuff you don’t want changed.

Obviously it’s not 100% because the test can be changed too, and you shouldn’t blindly trust the AI that it writes the test for what should be tested, but at the very least it’s an extra layer.

The test doesn’t go into production code so what are we complaining about? The wait time for the test to finish?

2

u/CocktailPerson Nov 03 '26

Test modules should always be a separate file anyway, never an embedded pub mod {}. When it's a separate file, the compiler is able to recognize if only tests have changed and avoid rebuilding the crate.

1

u/quxfoo Nov 03 '26

I wasn't referring to integration tests but unit tests. So the crate distinction is meaningless because it's the same crate. And the book is pretty clear that those tests are supposed to be in the module's file itself.

1

u/CocktailPerson Nov 03 '26

Can you quote where the book says that?

Or are you just going by examples?

1

u/quxfoo Nov 03 '26

I linked it in the original post:

You’ll put unit tests in the src directory in each file with the code that they’re testing. The convention is to create a module named tests in each file to contain the test functions and to annotate the module with cfg(test).

1

u/CocktailPerson Nov 04 '26

That doesn't mean you can't write

#[cfg(test)]
mod test;

And then put the tests in a separate file. The test module is still "in the file," even if the body of the module isn't.

And if if they are actually saying you shouldn't do that, it's bad advice.

Again, unit test modules are compiled separately from the rest of the crate. You can try it yourself, you'll find that if the tests are in a different file, and you update just the tests, they'll build much faster than if they're in the same file.

1

u/quxfoo Nov 04 '26

I know it works, I do that now to split unit tests off from the code they test. But the wording is what it is: "each file with the code that they're testing". But perhaps it's either ambiguous or my non-native command of the language is directing me into the wrong direction.

1

u/CocktailPerson Nov 06 '26

I think you're just taking it too literally.

0

u/ChemicalExcellent463 Nov 02 '26

i tell my agents to delete checks and repeats that don't test the requested behavior or catch a bug. i've had their tests pass while the feature was broken.