Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to use with test-env-log ? #69

Closed
pinage404 opened this issue Oct 5, 2021 · 4 comments
Closed

How to use with test-env-log ? #69

pinage404 opened this issue Oct 5, 2021 · 4 comments

Comments

@pinage404
Copy link

Hello,

i tried to use test-case with test-env-log but i got the error

error[E0659]: test is ambiguous (glob import vs any other name from outer scope during import/macro resolution)

use test_case::test_case;
use test_env_log::test;

#[test_case(4,  2  ; "when operands are swapped")]
#[test_case(-2, -4 ; "when both operands are negative")]
#[test_case(2,  4  ; "when both operands are positive")]
fn multiplication_tests(x: i8, y: i8) {
	let actual = (x * y).abs();

	assert_eq!(8, actual)
}
cargo test
   Compiling git-gamble v2.1.1-alpha.0 (/home/pinage404/Project/git-gamble)
error[E0659]: `test` is ambiguous (glob import vs any other name from outer scope during import/macro resolution)
   --> tests/test_env_log_with_test_case.rs:4:1
    |
4   | #[test_case(4,  2  ; "when operands are swapped")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ambiguous name
    |
note: `test` could refer to the attribute macro imported here
   --> tests/test_env_log_with_test_case.rs:4:1
    |
4   | #[test_case(4,  2  ; "when operands are swapped")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = help: consider adding an explicit import of `test` to disambiguate
    = help: or use `self::test` to refer to this attribute macro unambiguously
note: `test` could also refer to the attribute macro defined here
   --> /media/exec_downloaded/rust/rustup/toolchains/1.55.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/prelude/mod.rs:119:13
    |
119 |     pub use super::v1::*;
    |             ^^^^^^^^^^^^
    = note: this error originates in the attribute macro `test_case` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0659`.
error: could not compile `git-gamble` due to previous error

How to use with test-env-log ?

related d-e-s-o/test-log#18

maybe related #38

@luke-biel
Copy link
Collaborator

Hello,
nice to see our crate put to use.
I can see where your problem comes from, and test_case unfortunately doesn't support replacing #[test] attribute yet. However, what @d-e-s-o has mentioned in the linked issue is a solution here (it's in async section, thus I can understand confusion, the feature was initially introduced to work with async tests).
I've managed to get the example working via replacing import with an explicit path:

use test_case::test_case;

#[test_case(4,  2  ; "when operands are swapped")]
#[test_case(-2, -4 ; "when both operands are negative")]
#[test_case(2,  4  ; "when both operands are positive")]
#[test_env_log::test]
fn multiplication_tests(x: i8, y: i8) {
    let actual = (x * y).abs();

    assert_eq!(8, actual)
}

This way test_case can internally expand multiplication_tests into 3 separate methods first, and after that test_env_log can add it's extras.

If this solves your problem, please close this issue

@pinage404
Copy link
Author

Hello,

I had not tested in this order

Your solution solves my problem

Thank you !

@newsch
Copy link

newsch commented Jun 27, 2022

Thanks for this useful crate!

When I use the latest test-case and test-log (new name of test-env-log) together, I get duplicate copies of each test generated.
I also tried it with what I think were the versions from when this issue was created and have the same problem.

Full example in this gist

#[cfg(test)]
mod tests {
    use test_case::test_case;

    #[test_case(0, 0 ; "the same")]
    #[test_case(0, 1 ; "different")]
    #[test_log::test]
    // #[test_env_log::test]
    fn equality(a: usize, b: usize) {
        log::info!("a: {}", a);
        log::info!("b: {}", b);
        assert_eq!(a, b)
    }
}
Output
running 4 tests
test tests::equality::the_same ... ok
test tests::equality::the_same ... ok
test tests::equality::different ... FAILED
test tests::equality::different ... FAILED

failures:

---- tests::equality::different stdout ----
thread 'tests::equality::different' panicked at 'assertion failed: `(left == right)`
  left: `0`,
 right: `1`', main.rs:18:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- tests::equality::different stdout ----
thread 'tests::equality::different' panicked at 'assertion failed: `(left == right)`
  left: `0`,
 right: `1`', main.rs:18:9


failures:
    tests::equality::different
    tests::equality::different

test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--bin main'

@luke-biel
Copy link
Collaborator

Hmm, that's definitely not a desired result, must've slipped in testing. I'm gonna create a separate bug issue for this.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants