# Exclude test from being cached?

**URL:** <https://ziggit.dev/t/exclude-test-from-being-cached/2275>\
**Category:** Help\
**Tags:** build-system, testing\
**Created:** [November 26, 2023, 2:26pm UTC](https://ziggit.dev/t/exclude-test-from-being-cached/2275 "2023-11-26T14:26:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FObersteiner](https://ziggit.dev/user_avatar/ziggit.dev/fobersteiner/32/1177_2.png) [@FObersteiner](https://ziggit.dev/u/FObersteiner)\
**Post date:** [November 26, 2023, 2:26pm UTC](https://ziggit.dev/t/exclude-test-from-being-cached/2275/1 "2023-11-26T14:26:14Z")

</div>

Can I exclude a specific test from being cached, so that it’s called every time I trigger tests from the build.zig?

When running tests, the result gets cached normally, meaning as long as you don’t change the code that gets tested, the test is not called again. Normally, that makes perfect sense but when I was contributing to [zBench](https://github.com/hendriknielaender/zBench) lately, the question came up how we can integrate benchmarks as part of a tests. Here, you want to be able to call the test again, without necessarily having to change the source code that gets benchmarked (“tested”).

The alternatives I see at the moment are

- build the test, i.e. make a binary that you can run again. Seems kind of ok to me, however making binary from tests? Should be a throw-away thing, not in zig-out/bin…
- dump the full cache to a throw-away directory, e.g. with `zig build tests --cache-dir $(mktemp -d)`. Seems overkill to me, and I see this becoming slow as a project grows…

related:  
[https://ziggit.dev/t/how-to-enable-more-logging-and-disable-caching-with-zig-build-test/](https://ziggit.dev/t/how-to-enable-more-logging-and-disable-caching-with-zig-build-test/)

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [November 26, 2023, 3:00pm UTC](https://ziggit.dev/t/exclude-test-from-being-cached/2275/2 "2023-11-26T15:00:32Z")

</div>

Maybe you could generate a dependency with random data and make your benchmark “test” depend on that, you could even use that random data as a seed value in case you use any randomness in your tests.  
Then a nice thing might be to add a build option that lets you specify the seed value and make it so that it is only randomly initialized if you don’t provide it.  
That would mean that your builds are deterministic and caching per seed value, just if you don’t specify a seed value a new one gets generated every time.

[build-system dealing-with-one-or-more-generated-files](https://ziglang.org/learn/build-system/#dealing-with-one-or-more-generated-files)

I haven’t played that much with the build system, so I still don’t have that much practical experience, else I would create an example. But I think this should be possible.

---

<div class="post-metadata">

**Author:** ![FObersteiner](https://ziggit.dev/user_avatar/ziggit.dev/fobersteiner/32/1177_2.png) [@FObersteiner](https://ziggit.dev/u/FObersteiner)\
**Post date:** [November 28, 2023, 4:37pm UTC](https://ziggit.dev/t/exclude-test-from-being-cached/2275/3 "2023-11-28T16:37:10Z")

</div>

something like

```zig
const RndGen = std.rand.DefaultPrng;

test "random" {
    var rnd = RndGen.init(@as(u64, @intCast(std.time.timestamp())));
    const some_random_num = rnd.random().int(i32);
    std.debug.print("random number is {}", .{some_random_num});
}

```

won’t work btw. - the caching works very well 😉 I guess it just registers “test ‘random’ passed”, so as long as you don’t change anything upstream, nothing happens if you call for the test again. So I would have to go for a more sophisticated solution.

---

<div class="post-metadata">

**Author:** ![nurpax](https://ziggit.dev/user_avatar/ziggit.dev/nurpax/32/1217_2.png) [@nurpax](https://ziggit.dev/u/nurpax)\
**Post date:** [January 2, 2024, 11:10am UTC](https://ziggit.dev/t/exclude-test-from-being-cached/2275/4 "2024-01-02T11:10:57Z")

</div>

This doesn’t quite answer your question but IMO a practical alternative is to treat benchmarks completely separate from unit tests. In this approach you wouldn’t use the `test` keyword at all to write benchmark cases but would rather implementing your own mini framework with benchmark specific reporting. Is there any benefit to spawning benchmark cases from within `test` sections?

---

<div class="post-metadata">

**Author:** ![FObersteiner](https://ziggit.dev/user_avatar/ziggit.dev/fobersteiner/32/1177_2.png) [@FObersteiner](https://ziggit.dev/u/FObersteiner)\
**Post date:** [January 2, 2024, 11:30am UTC](https://ziggit.dev/t/exclude-test-from-being-cached/2275/5 "2024-01-02T11:30:16Z")

</div>

The idea was that benchmarks test performance, and based on that relation I would expect them alongside other tests. I don’t think there is a technical benefit to write benchmarks as “tests” using Zig’s test framework (at least not at the moment).

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [January 2, 2024, 1:03pm UTC](https://ziggit.dev/t/exclude-test-from-being-cached/2275/6 "2024-01-02T13:03:45Z")

</div>

I recently read this issue and I think it gives some context/reason why zig seems to go the route of benchmarks not being tests: [Benchmark Functions · Issue #1010 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/1010#issuecomment-840259973)

---

<div class="post-metadata">

**Author:** ![nurpax](https://ziggit.dev/user_avatar/ziggit.dev/nurpax/32/1217_2.png) [@nurpax](https://ziggit.dev/u/nurpax)\
**Post date:** [January 2, 2024, 2:42pm UTC](https://ziggit.dev/t/exclude-test-from-being-cached/2275/7 "2024-01-02T14:42:15Z")

</div>

I agree with that GitHub comment. In my experience, integrating benchmarks into “automated tests” (for example, letting a CI test outcome go red if benchmarks don’t hit their targets) has often lead to problems. Why? Well, because benchmarks tend to be flaky and thus you start getting all sorts of intermittent failures.
