-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Stabilize unit tests with non-()
return type
#48854
Comments
@rfcbot fcp merge I propose that we further stabilize unit tests etc that return non- |
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged teams: Concerns: Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Thanks for including the test links! Super helpful. I think letting This one surprised me, though: #[test]
#[should_panic]
fn not_a_num() -> Result<(), ParseIntError> {
let _: u32 = "abc".parse()?;
Ok(())
} I get cognitive dissonance from that, because I my brain says "what do you mean parse should panic? I thought the point was that it doesn't panic!", and I think it would be unfortunate if the test were to continue to pass should it actually start panicking. I think I was expecting
(Unimportant thought: a |
#[test]
#[should_panic]
fn panic_panics() -> ! { panic!() } 😛 |
I think this definitely warrants eventually adding an alias for |
cc @jonhoo re: test frameworks |
@sgrif Note that I'm explicitly not suggesting that it should be an alias: I would like |
@scottmcm Fair point. I agree. |
In the context of custom test frameworks, this seems fine. The path we're taking going forward is that what ships with Rust by default is the same thing that Note that with ctfs, new test frameworks can implement entirely new testing annotations, and are not bound to the annotations, signatures, and semantics as the current built-in test support. Also cc @Manishearth . |
@rfcbot concern experience Has this had much use? It's the first I knew about it even being a thing (I've not been following the |
Yeah, I didn't even know this had landed on nightly yet? Super excited about the feature though! |
I don't think it's had much experience. I agree with @scottmcm that I'm ok with waiting a bit, although it makes stabilizing the "main" case a bit harder, since we have to split the two. But I would encourage those who have doubts about the feature to experiment -- there really isn't much more to it, I don't think. In other words: under this feature, there are really three (and a half) distinct possible outcomes:
I'm personally not inclined to subdivide the final case: I feel like if you want to test for a specific return code, you ought to just add an (One other reason for holding back on this feature: a major motivation -- perhaps the motivation -- was using it from within rustdoc, so that those examples can show idiomatic code -- I'm not sure if that works yet or what it takes to make it work.) |
@rfcbot fcp cancel I'm going to cancel the FCP. I added @scottmcm's concern to the header, as well as the desire to see a rustdoc example in practice. =) It might however be hard to make Thinking more about it, I'm not sure how desirable it is. I agree |
@nikomatsakis proposal cancelled. |
I'd use stronger language than "confusing". If the only test framework we can use (and will forever be the default test framework) says that "returning a Tests are something that should check specific conditions. #[test]
#[should_panic(expected = "index out of bounds")]
fn e() {
let a = [1, 2, 100];
let b = a[3]; // oops
let c = t(&a, b);
}
fn t(a: &[usize], idx: usize) {
a[idx];
} It's my belief that #[test]
fn e() {
let a = [1, 2, 100];
let b = a[3]; // oops
assert_that(|| {
let c = t(&a, b);
}).panics()
.with("index out of bounds")
} IMO, returning a assert!(f.is_err()); |
Hmm. Plausible. Maybe that's how we should refactor the test runner's internal workings, actually. That is, we could refactor |
I've been thinking about this a bit. Here is one simple possible design:
Under this version, if we ignore
With Thoughts? |
I think this is the right mindset. I've always envisioned |
That sounds great, @nikomatsakis; it resolves all my concerns about And it allows the great "replace |
@rfcbot reviewed |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
The final comment period is up, let's do this! There are general instructions on how to stabilize a feature given here: https://forge.rust-lang.org/stabilization-guide.html In this case, the feature name in question is |
hi @nikomatsakis I can work on this |
…t, r=nikomatsakis Stabilize unit tests with non-`()` return type References rust-lang#48854
…t, r=nikomatsakis Stabilize unit tests with non-`()` return type References rust-lang#48854
…t, r=nikomatsakis Stabilize unit tests with non-`()` return type References rust-lang#48854
Since #51298 is merged, we can close this issue? |
Pretty sure this can be closed now that this is stabilized, thanks @Dylan-DPC! |
I'm missing something. I went to use this, but what's all this junk about #[test]
fn example() -> Result<(), Box<dyn std::error::Error>> {
Err(String::from("boom"))?;
Ok(())
} Output:
|
@shepmaster I think there is a PR at #52453 to fix that. |
Thanks. It's a shame this will go out in the suboptimal form in 1.28, and presumably 1.29, but hopefully we will see it in 1.30! |
Very late to the party! Sorry! @nikomatsakis wrote at the top:
Why? I wrote a test runner for a complicated test setup and tear-down use case using In other words, I wrote a test where I return
Meanwhile, I commented out this test because it does not compile. I have to admit that I am confused and probably what I am trying is based on faulty assumptions, however the question remains: Why is |
@nalply I think the answer is #48854 (comment) but I'd love clarification on this also. |
CURRENT STATUS:
Awaiting someone to write the stabilization PR! Mentoring instructions here.
This is a sub-issue of the larger tracking issue devoted to the
?
in main RFC. This issue corresponds to stabilizing the use of unit tests whose return type is something other than()
-- it basically an extension of #48453, which was discussing the same thing but for themain
function.What would be stabilized
As before, unit tests that return
()
:#[should_panic]
However, unit tests can now have other return types:
Termination
traitOk
fromResult
)()
, then#[should_panic]
is disallowedUnknowns
#[should_panic]
and#[bench]
anyway? (Example)Older proposal
Possible changes needed before stabilizing
#[should_panic]
from#[should_error]
, as suggested by @scottmcm?What would be stabilized
Unit tests (and
#[bench]
functions) would join themain
function in being allowed to return any value that implements theTermination
trait.This commits us to the following (each link is to a test):
Result
and otherTermination
types. In the case ofResult
:Ok
means test passesErr
causes test failurereport()
, although that detail is not being stabilized#[bench]
is still effectively unstable though in generalWhat remains unstable
The text was updated successfully, but these errors were encountered: