#Tests in separated module

1 messages · Page 1 of 1 (latest)

brittle patrol
#

tree:

src
├── main.zig
├── protocol
│   ├── deserializer.zig
│   ├── handler.zig
│   ├── serializer.zig
│   └── types.zig
├── server
│   ├── access_control.zig
│   ├── cli.zig
│   ├── cmd_handler.zig
│   ├── config.zig
│   ├── connection.zig
│   ├── employer.zig
│   ├── err_handler.zig
│   ├── listener.zig
│   ├── logger.zig
│   ├── persistance.zig
│   ├── request_processor.zig
│   ├── storage.zig
│   ├── stream_server.zig
│   ├── tracing.zig
│   ├── utils.zig
│   └── worker.zig
└── tests
    ├── protocol
    │   ├── deserializer.zig
    │   ├── handler.zig
    │   └── serializer.zig
    ├── run.zig
    ├── server
    │   ├── access_control.zig
    │   ├── cmd_handler.zig
    │   ├── config.zig
    │   ├── err_handler.zig
    │   ├── logger.zig
    │   ├── persistance.zig
    │   ├── storage.zig
    │   └── utils.zig
    └── test_helper.zig
amber imp
#

this is really not the way tests are designed to be used in zig (you should put them in the file they're testing), but if you really, really want to do this, there's two ways:

  1. put a test.zig in src that imports all your tests, and make that the root source file for your test step
  2. expose main.zig as a module to your test step using addAnonymousImport (or just addImport if you happen to be exposing it as a module anyway)
#

option 1 requires your tests to be nested under src, not outside of it, and option 2 requires that they only test functions made public from your module's entrypoint, so they're both a bit limited, which is why it's not really recommended to do testing this way

brittle patrol
#

this is really not the way tests are designed to be used in zig (you should put them in the file they're testing)

I know but the tests aren't trivial that's why i moved out to separeted module

amber imp
#

(fwiw > quote goes here is how you do quotes on discord :)

looks like this

amber imp
#

You can see files in std that are like half tests

brittle patrol
#

But my tests are more like integrational tests

amber imp
#

Zig also places much less emphasis on keeping files small than other languages. Sema.zig in the compiler is a great example of this - it's 30k lines of code, which seems insane, but it actually makes sense because splitting it into smaller chunks would require arbitrary subdivision of something that, conceptually, is a single unit

brittle patrol
#

and still if i put my tests in the same file everything will be a clusterfuck

amber imp
#

You should still keep unit tests in the files they test though

brittle patrol
#

Yeah, I have some unit tests and i should move them into implementation file

amber imp
#

And you can then import your integration tests using a test { _ = @import("integration_tests/root.zig") } or whatever inside your regular main.zig

brittle patrol
#

Okay, created test.zig file and set it to test root in build.zig now i got a problem it's not seeing my deps

src/tests/server/cmd_handler.zig:364:30: error: root struct of file 'tests.server.config' has no member named 'load'
    const config = try Config.load(std.testing.allocator, null, null);
                       ~~~~~~^~~~~
src/tests/server/cmd_handler.zig:403:30: error: root struct of file 'tests.server.config' has no member named 'load'
#
src
├── main.zig
├── protocol
│   ├── deserializer.zig
│   ├── handler.zig
│   ├── serializer.zig
│   └── types.zig
├── server
│   ├── access_control.zig
│   ├── cli.zig
│   ├── cmd_handler.zig
│   ├── config.zig
│   ├── connection.zig
│   ├── employer.zig
│   ├── err_handler.zig
│   ├── listener.zig
│   ├── logger.zig
│   ├── persistance.zig
│   ├── request_processor.zig
│   ├── storage.zig
│   ├── stream_server.zig
│   ├── tracing.zig
│   ├── utils.zig
│   └── worker.zig
├── test.zig
└── tests
    ├── protocol
    │   ├── deserializer.zig
    │   ├── handler.zig
    │   └── serializer.zig
    ├── run.zig
    ├── server
    │   ├── access_control.zig
    │   ├── cmd_handler.zig
    │   ├── config.zig
    │   ├── err_handler.zig
    │   ├── logger.zig
    │   ├── persistance.zig
    │   ├── storage.zig
    │   └── utils.zig
    └── test_helper.zig
#

test.zig

comptime {
    // protocol
    _ = @import("tests/protocol/handler.zig");
    _ = @import("tests/protocol/serializer.zig");
    _ = @import("tests/protocol/deserializer.zig");

    // server
    _ = @import("tests/server/cmd_handler.zig");
    _ = @import("tests/server/err_handler.zig");
    // _ = @import("src/server/listener.zig");
    _ = @import("tests/server/config.zig");
    _ = @import("tests/server/storage.zig");
    // _ = @import("server/tracing.zig");
    // _ = @import("src/server/cli.zig");
    _ = @import("tests/server/logger.zig");
    _ = @import("tests/server/utils.zig");
    _ = @import("tests/server/persistance.zig");
    _ = @import("tests/server/access_control.zig");
}
#

oh i see that its trying to import those deps from tests.server.config?? wtf