Open Jarred-Sumner opened 2 years ago
Can you elaborate what parts of this are not redundant to implementing tests (for groups of code) inside zig build
?
What would be the best practice to communicate to new contributors how things must be tested, if they fix a local bug (without having looked into the build system)? As of now, there is no convention.
No standardization: every project will have a slightly different implementation with different behavior and bugs.
Every project has different constraints on logical dependency of how tests are build. Think of tests requiring external dependencies, fetching up-to-date data from the system etc. This point is more of a question, how this should be communicated.
zig build cache invalidates frequently
This should be (de)motivated by a use case. I dont understand the performance implications for different project sizes. Maybe you can also argue, how this can not be fixed in the caching system?
Two ways to run tests is more confusing than one way.
That is a point against using zig test
, as zig test
cant enforce updating+building project build dependencies etc. Projects can also have special test logic (ie complete repos/subprojects for testing complicated stuff). (Remind: The package manager is for fetching packages.)
zig build sounds like it's for building, not for testing.
Many projects invoke automatic tests after building or making sure dependencies are build etc. Take arocc.
Sidenode: Tests are also build, but then "run". One could distinguish here within the build system, but in practice users expect them to be run immediately on default.
Two ways to run tests is more confusing than one way.
That is a point against using
zig test
, aszig test
cant enforce updating+building project build dependencies etc. Projects can also have special test logic (ie complete repos/subprojects for testing complicated stuff). (Remind: The package manager is for fetching packages.)
As someone who is new to Zig having two ways to run tests has been confusing, but I understand why both exist. It does seem like zig test
can't service all the use cases that zig build test
satisfies, since projects can have more complex setups that the build.zig
defines and running zig test
would ignore all that setup. However, zig test
also has features and options that zig build
steps that contain tests don't get, like command line options for filtering, etc.
It occurs to me that if we made it easier to replicate all the zig test
functionality in a build step, with maybe a standard set of options that could be attached to a test build step, and if those set of options were expanded to include filters or selectors like the proposal specifies, then zig build test
would be a better default option.
I'm very much in favor of the entire proposal! I think the fact that there are entire blog posts and threads on Reddit about zig test
not running all tests, clearly indicates that there is a demand and there needs to be done something.
It would be nice if zig test
or zig build test
could explain why some of the tests weren't run (e.g "not reached" or "not evaluated") given that zig still has to parse all files and is aware of all tests present within them.
This would likely reduce the confusion when it looks as if only three tests were picked up when you have 30 or so tests in other files.
Currently, if you pass multiple files to
zig test
, it fails with this error:Say you have many files with tests in them, and you want to run all the tests for those files.
For test runners in other languages, the command for running tests in one file is the same for running tests in several files.
Rust:
Go:
JavaScript:
Why not
zig test main.zig
?You can run
zig test
on the entry point, but it's a footgun.zig test
silently skips tests in files not referenced by runtime code. That means it reports all tests passed without necessarily running all the tests. This is probably an unintentional side effect of zig's fantastic dead code elimination.Why not
zig build test
?zig build
cache invalidates frequently, making it several seconds slower than manually invokingzig test
on the target filezig build
sounds like it's for building, not for testing.Proposal
Part 1
Support multiple file paths as input in
zig test
:This would run the tests in those files.
Note: I am not suggesting a glob matcher. It would be nice, but the shell can do that.
Part 1A
Change the default behavior of
zig test
to only runtest { }
andtest ".*" { }
for the input file(s), instead of all files referenced recursively.This would only run the
test
blocks defined insrc/file1.zig
. Ifsrc/file1.zig
imports another file withtest
blocks in them, those tests would not be run.This change would:
zig test
src/file1.zig
, I don't want to see test failures or build failures from tests insrc/strings.zig
.The current way to do this is by adding a prefix to every test name and then passing that prefix to
zig test
. Humans shouldn't have to add a prefix to every test's name.Part 1B
Being able to run all tests for an entry point is very useful.
Rather than removing that, this proposal suggests changing it from the default behavior to a flag.
Here are two ideas for flag names that come to mind:
Part 2
Add support for
build.zig
to specify default options forzig test
, without wrappingzig test
aszig build test
Why? Currently, projects with dependencies can't run
zig test
without adding a lot of flags.My current incantation for running a test looks something like this:
This proposal does not go into the details of what the API would look like for
build.zig
to specify default options forzig test
.Summary
If this entire proposal were to be implemented:
Other
This is the script I currently use to generate a file that imports all files with tests, regardless of whether the test files are referenced by runtime code in the real entry point.