# Cmdtest - CLI testing for Zig

**URL:** <https://ziggit.dev/t/cmdtest-cli-testing-for-zig/12895>\
**Category:** Showcase\
**Tags:** build-system, cli\
**Created:** [November 7, 2025, 5:32am UTC](https://ziggit.dev/t/cmdtest-cli-testing-for-zig/12895 "2025-11-07T05:32:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyk](https://ziggit.dev/user_avatar/ziggit.dev/pyk/32/6807_2.png) [@pyk](https://ziggit.dev/u/pyk)\
**Post date:** [November 7, 2025, 5:32am UTC](https://ziggit.dev/t/cmdtest-cli-testing-for-zig/12895/1 "2025-11-07T05:32:59Z")

</div>

Hey guys, I have published a new package called [cmdtest](https://github.com/pyk/cmdtest).

`cmdtest` is my personal approach to write integration tests for my Zig CLI apps and I thought I’d share it for everyone to use.

Here’s how to use it:

1. Fetch the latest release:

2. Write your test file. Example: `test/mycli.zig`.

3. Register the test in `build.zig`:

4. Run the tests:

* * *

I also wrote a blog post, like a “behind the scene” look at the things that I have learned while publishing this package. It covers Zig I/O, comptime vs runtime, and `build.zig` imports. If you’re interested, you can read it [here](https://pyk.sh/blog/2025-11-07-cmdtest-v0-2-0)

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [November 7, 2025, 8:28am UTC](https://ziggit.dev/t/cmdtest-cli-testing-for-zig/12895/2 "2025-11-07T08:28:48Z")

</div>

You might be interested in this thread:

> [@How to write integration tests for CLI utilities?](https://ziggit.dev/t/how-to-write-integration-tests-for-cli-utilities/2806):
>
> Let’s say I am writing a CLI utility, which, say, prints hello world: // ./main.zig const std = @import("std"); pub fn main() !void { try std.io.getStdOut().writeAll("hello world"); } Now, I want to write an integration test to run the utility and check its output, something like this: // ./tests.zig const std = @import("std"); test "it prints hello world" { const exe\_path: []const u8 = get\_exe\_path(); const exec\_result = try std.ChildProcess.exec(.{ .allocator = std.tes…

---

<div class="post-metadata">

**Author:** ![pyk](https://ziggit.dev/user_avatar/ziggit.dev/pyk/32/6807_2.png) [@pyk](https://ziggit.dev/u/pyk)\
**Post date:** [November 15, 2025, 2:30am UTC](https://ziggit.dev/t/cmdtest-cli-testing-for-zig/12895/3 "2025-11-15T02:30:12Z")

</div>

I’ve pushed some [changes](https://github.com/pyk/cmdtest/commit/e3c67bef7a3bb9cf5bf1088fea83aed541cb0fcb) to `main` branch. This changes allows you to test interactive CLI like this:

```zig
test "spawn: interactive mode" {
    const argv = &[_][]const u8{ "cmdtest", "--interactive" };
    var proc = try cmdtest.spawn(.{ .argv = argv });
    defer proc.deinit();

    try proc.writeToStdin("PING\n");
    try testing.expectEqualStrings("PONG", try proc.readLineFromStdout());

    try proc.writeToStdin("ECHO works\n");
    try testing.expectEqualStrings("works", try proc.readLineFromStdout());

    try proc.writeToStdin("EXIT\n");

    const term = try proc.child.wait();
    try testing.expectEqual(@as(u8, 0), term.Exited);
}

```
