-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: companion file for the tests #608
Comments
have a look at std/special/compiler-rt/* |
@andrewrk: such tests do not have access to non-public functions/internal structures. It is easy to test purely functional math API, but how would it deal with a complex library? |
My suggestion: write functional tests (meaning tests that only use the public api) in a separate file, and try to get 100% coverage (#352). Then when you make a new version of the library, make sure your coverage hasn't regressed. If there are parts of your library that are infeasible to get coverage on in this way, you could write private-access unit tests in the source file like what is already possible. |
Been there, done that. It is very exhausting and IMHO not worth the blood and tears. The problem is that the most useful tests deal directly with internal parts (to set up edge cases). Moving these specific tests into separate file benefits readability most. |
An aside note. Companion files could be also useful for other purposes. One of the "good" ideas I once got was to add internal HTTP server to the application, to show internal state of various program structures at any moment. It didn't go well. It was virtually impossible to ensure that every displayed internal structure is made MT safe. Later I switched to generating HTML files for the whole app when demanded. This was much easier solution (no need to add MT safety everywhere) and mildly usable. However, generating single HTML page could easily take thousands of LOC for nontrivial data structure. By definition the generator code needed full access to everything, so it could not be placed in a separate file. If Zig has a way to define "this is how specific companion file for my project looks like" it would make such features less cumbersome. |
I believe some prog languages put test cases (or pre- and post-conditions) in the comment header of functions. This does not hurt readability of the actual code too much. |
The use case I am thinking about has 50+% of code inside tests. This quickly becomes hard to read and manipulate. Especially tests of internal parts are long and hairy and would be better separated. However, this requires direct access to internals from that separate file. Examples could be handled separately (and not in comments - hard to copy/paste): #507. |
tl;dr:
When there's source file
abc.zig
(the master) then there could also beabc.zig-tests
(test companion) file containing tests. The compiler will understand this arrangement.There are few problems with test heavy code. It takes lot of effort, for example. Another trouble is: where to put all these tests?
Tests could be placed:
What are advantages and disadvantages of these schemes?
My proposal:
name.zig
and alsoname.zig-tests
, then this test companion is automatically compiled together with the master file.zig
source can import it.Optional feature is to allow easier orientation in the tests. There could be "test forwards":
Every "test forward" would need to have unique name (within the source file) and this name has to be present exactly once in test companion file.
Such feature would allow the reader to locate relevant tests and give a hint about test coverage.
This proposal can work together with the proposal for custom test runner #567.
The text was updated successfully, but these errors were encountered: