The bug reported in #12383 made me aware of the convention that tests in non-std libraries often reside in the same file as the code under test. The privacy issue seen in that bug appears to be an immediate consequence of this convention. The convention also leads to pretty large source files that are imho difficult to handle.
Thus I would like to discuss whether this convention should be changed. To suggest a concrete alternative:
lib.rs
; the test module may have substructure depending on the complexity of the library being testedI don't think this is necessary, and I think there is value to having tests live next to the unit they are testing. Out-of-crate tests are fine too, we can add those separately in src/test/run-pass.
IMO, the tests should be separate from the code. Oftentimes, the LOC for tests can get larger than the library itself. I like what Go does, where each source file foo.go
has its own unit testing code in foo_test.go
. go test
just looks for these *_test.go
files and runs them all against their respective source files. You can do something similar with Rust if you declare a module at the bottom of the source file and define the testing module in its own file, but then you have to import everything you want to test from the parent module. IMHO, this feels very clunky. I should at least _be able to_ separate my tests from the code in my library if I want to.
/cc @aturon , man of many conventions.
@dead10ck I think this is good use case for glob imports; just have the test use module_that_i_am_testing::*
. I don't like the way Golang mashes all .go
files in the same level of the directory structure into the same namespace.
Conventions changes go through the RFC process today, and so I'm giving this issue a close. Please open an RFC if you'd like to persue this further. Thanks.
@steveklabnik Was there ever an RFC? If not, is there now a standard way to separate tests?
@steveklabnik I am also interested in an answer to this.
Seeing this popping up now again in my inbox. I never opened an RFC for this and kind of got used to it by now ;)
I truly believe that giving a better API/Option to write tests outside of the same file would be extremely beneficial for RUST language as whole. It would reduce the barrier for other adopters as well as ensure file size can be kept small without sacrificing on reducing documentation/testing.
I really would love if this issue could be reconsidered.
Has there been any progress on this (RFC)?
Most helpful comment
@steveklabnik Was there ever an RFC? If not, is there now a standard way to separate tests?