Skip to content
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

Make private const/vars/function visible for testing #2852

Open
DrDeano opened this issue Jul 8, 2019 · 2 comments
Open

Make private const/vars/function visible for testing #2852

DrDeano opened this issue Jul 8, 2019 · 2 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@DrDeano
Copy link
Contributor

DrDeano commented Jul 8, 2019

When running under zig test.
When separating out the unit tests from the main source file, have the private const/vars/function visible for the testing file that is testing the source file. For example:

src.zig:

const some_value: u32 = undefined;

pub fn init() void {
    some_value = 100;
}

test_src.zig:

const src = @import("src.zig");
const expectEqual = @import("std").testing.expectEqual;

test "some_value set after init()" {
    src.init();
    expectEqual(100, src.some_value);
}
@ghost
Copy link

ghost commented Jul 8, 2019

You could also argue for only allowing public members to be visible inside tests, because if you test private members, you might end up testing implementation instead of testing behavior, and that's bad if you later want to refactor or switch to a new implementation.

The most important thing is consistency though, no matter which file the test resides in.

See:
https://teamgaslight.com/blog/testing-behavior-vs-testing-implementation

https://softwareengineering.stackexchange.com/questions/274937/is-it-bad-practice-to-make-methods-public-solely-for-the-sake-of-unit-testing

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jul 12, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Jul 12, 2019
@PavelVozenilek
Copy link

I'd proposed this once in #608. Ability to test the internals is very useful, if one is into defensive coding.

@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 10, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants