Skip to content

Rename #[should_fail] to #[should_panic] #21824

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

Merged
merged 1 commit into from
Mar 9, 2015

Conversation

sfackler
Copy link
Member

No description provided.

@rust-highfive
Copy link
Contributor

r? @gankro

(rust_highfive has picked a reviewer for you, use r? to override)

@steveklabnik
Copy link
Member

This is intentional. Tests pass or fail. That the test fails because of a panic is an implementation detail.

@sfackler
Copy link
Member Author

sfackler commented Feb 1, 2015

Inflating "not panicing" with the test failing seems wrong, for exactly this case where you're expecting a panic for the test to succeed.

@steveklabnik
Copy link
Member

Inflating "not panicing" with the test failing seems wrong,

Sorry, it's late. My point is just this: the panic aspect is implementation. Semanticly, tests pass or fail, they don't pass or panic. Hence should_fail.

@sfackler
Copy link
Member Author

sfackler commented Feb 1, 2015

But #[should_fail]/#[should_panic] happens before the determination of a test succeeding or failing. It in fact defines the conditions under which a test succeeds or fails. If I write this:

#[test]
#[should_panic(expected = "out of bounds")]
fn test_oob() {
    vec![][10]
}

I'm saying "I expect a panic to occur when running this test, and the string associated with the panic to contain "out of bounds". The test should succeed if and only if that happens." The attribute is explicitly tied to the panic system. The equivalent in JUnit is @Test(expected=OutOfBoundsException.class) which also avoids the term "failure".

To me, #[should_fail] is saying "this test only succeeds if it fails", which which seems pretty paradoxical.

@sanxiyn
Copy link
Member

sanxiyn commented Feb 1, 2015

I don't think should_fail is a good name because it may be confused with xfail mechanism in other testing frameworks. It is completely different from xfail in that the test is expected to pass, not fail.

@steveklabnik
Copy link
Member

To me, #[should_fail] is saying "this test only succeeds if it fails", which which seems pretty paradoxical.

Only as paradoxial as !. It's just inverting the condition. You expect this test to fail, instead of pass.

@sfackler
Copy link
Member Author

sfackler commented Feb 1, 2015

So I was under the impression that this was just a missed rename from the fail -> panic switch. If it's intentional, then this should probably move to an RFC.

@steveklabnik
Copy link
Member

The fail -> panic RFC mentioned nothing about the attribute at all, and we had a momentary discussion of "should this attribute change too?" and didn't do it, basically for the reason I cited. So yeah, maybe this should sit on an RFC instead.

@alexcrichton
Copy link
Member

I would personally agree with @sfackler here because we have precisely one method of determining whether a test has passed or failed, which is a panic. I agree with @steveklabnik that if we had multiple methods to fail a test then #[should_fail] should probably be the name of the attribute.

@steveklabnik, knowing that there's only one method of "failing a test", aka panicking, does that sway your thoughts one way or the other?

@steveklabnik
Copy link
Member

Basically my thoughts are this:

  1. should_panic describes an implementation detail, should_fail describes a semantic. We should name things after thier semantics, not their internals.
  2. I don't really care that strongly to seriously argue about it if everyone else feels differently than I do.

@sfackler
Copy link
Member Author

sfackler commented Feb 1, 2015

In my opinion, the semantic vs implementation detail descriptions are actually the other way around. If I have a function that documents that it panics if some condition happens, I want to tell the test framework to expect specifically a panic, maybe with some string attached. Keep in mind that assert! and assert_eq! are general purpose macros whose semantic purpose is to panic, as opposed to macros designed specifically for test cases like the static methods in JUnit's Assert class.

On the other hand, it may not necessarily be true in the future that panicking is the only way to cause a test to fail. Go's test infrastructure provides functions to indicate that the test will fail, without stopping execution (this is a bit like the difference between span_err and span_fatal in the compiler). It can be useful to have this variant if you're doing something like testing multiple inputs in one test functions, since you'll be able to see error messages for all of the inputs that broke instead of only the first.

@bombless
Copy link
Contributor

bombless commented Feb 1, 2015

More like

#[test]
#[should_pass]
fn test_oob() {
    assert_panic!("out of bounds", {        
        vec![][10]
    }); // if the block executed successfully, we panic
}

@sfackler
Copy link
Member Author

sfackler commented Feb 1, 2015

@bombless panics can't be caught, so that test would end up panicking and therefore failing in all cases.

@bombless
Copy link
Contributor

bombless commented Feb 1, 2015

@sfackler Need some compiler magic for the assert_panic macro, I guess.

@bombless
Copy link
Contributor

bombless commented Feb 1, 2015

Ah, I see, the real problem is the lack of should_panic, we'll need it anyway.

@pnkfelix
Copy link
Member

I'm inclined to agree with @alexcrichton that given that panic! (and wrappers around it like assert! and assert_eq!) are the way to signal failure, #[should_panic] makes sense.

(And it makes the tests easier to talk about: "This test failed because it is marked should_panic and did not panic", as opposed to "This test failed because it is marked should_fail and did not panic.")

(The only other way I can imagine a test failing is via a segmentation fault, but I assume that #[should_fail] test that segfaults is not supposed to be accepted by the test driver.)

@Gankra
Copy link
Contributor

Gankra commented Feb 21, 2015

What's the status on this? Are we going for an RFC?

@alexcrichton
Copy link
Member

We talked about this in today's meeting (minutes to be posted soon) and would like to move forward with this. r=me with a rebase, and thanks again @sfackler! (sorry about the delay)

@sfackler
Copy link
Member Author

Cool, will update tonight.

bors added a commit that referenced this pull request Feb 26, 2015
This needs to make it into the snapshot before #21824 can land.

r? @alexcrichton
@huonw huonw mentioned this pull request Mar 2, 2015
@huonw
Copy link
Member

huonw commented Mar 2, 2015

@bors r=alexcrichton a84a

@bors
Copy link
Collaborator

bors commented Mar 2, 2015

⌛ Testing commit a84a5ea with merge f649c3b...

@bors
Copy link
Collaborator

bors commented Mar 2, 2015

💔 Test failed - auto-linux-64-nopt-t

@Manishearth
Copy link
Member

/home/ubuntu/src/rust-buildbot/slave/auto-linux-64-nopt-t/build/src/libarena/lib.rs:324:1: 324:16 error: The attribute `should_panic` is currently unknown to the the compiler and may have meaning added to it in the future
/home/ubuntu/src/rust-buildbot/slave/auto-linux-64-nopt-t/build/src/libarena/lib.rs:324 #[should_panic]
                                                                                        ^~~~~~~~~~~~~~~
/home/ubuntu/src/rust-buildbot/slave/auto-linux-64-nopt-t/build/src/libarena/lib.rs:324:1: 324:16 help: add #![feature(custom_attribute)] to the crate attributes to enable
/home/ubuntu/src/rust-buildbot/slave/auto-linux-64-nopt-t/build/src/libarena/lib.rs:324 #[should_panic]
                                                                                        ^~~~~~~~~~~~~~~
/home/ubuntu/src/rust-buildbot/slave/auto-linux-64-nopt-t/build/src/libarena/lib.rs:339:1: 339:16 error: The attribute `should_panic` is currently unknown to the the compiler and may have meaning added to it in the future
/home/ubuntu/src/rust-buildbot/slave/auto-linux-64-nopt-t/build/src/libarena/lib.rs:339 #[should_panic]
                                                                                        ^~~~~~~~~~~~~~~
/home/ubuntu/src/rust-buildbot/slave/auto-linux-64-nopt-t/build/src/libarena/lib.rs:339:1: 339:16 help: add #![feature(custom_attribute)] to the crate attributes to enable
/home/ubuntu/src/rust-buildbot/slave/auto-linux-64-nopt-t/build/src/libarena/lib.rs:339 #[should_panic]
                                                                                        ^~~~~~~~~~~~~~~

@Manishearth
Copy link
Member

IIRC this did not make it into the snap. @flaper87 could you make another snapshot?

Manishearth added a commit to Manishearth/rust that referenced this pull request Mar 8, 2015
bors added a commit that referenced this pull request Mar 9, 2015
@Manishearth
Copy link
Member

@bors: retry

@bors
Copy link
Collaborator

bors commented Mar 9, 2015

⌛ Testing commit a84a5ea with merge 85d602f...

@bors
Copy link
Collaborator

bors commented Mar 9, 2015

💔 Test failed - auto-mac-32-opt

@Manishearth
Copy link
Member

/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:333:30: 335:6 error: `arena` does not live long enough
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:333     let result = arena.alloc(|| Outer {
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:334         inner: arena.alloc(|| Inner { value: 10 })
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:335     });
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:329:42: 338:2 note: reference must be valid for the block suffix following statement 1 at 329:41...
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:329     struct Outer<'a> { inner: &'a Inner }
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:330 
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:331     let arena = Arena::new();
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:332 
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:333     let result = arena.alloc(|| Outer {
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:334         inner: arena.alloc(|| Inner { value: 10 })
                                                                                       ...
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:331:29: 338:2 note: ...but borrowed value is only valid for the block suffix following statement 2 at 331:28
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:331     let arena = Arena::new();
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:332 
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:333     let result = arena.alloc(|| Outer {
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:334         inner: arena.alloc(|| Inner { value: 10 })
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:335     });
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/libarena/lib.rs:336 

Was that test perhaps removed?

@pnkfelix
Copy link
Member

pnkfelix commented Mar 9, 2015

Was that test perhaps removed?

Yes, in commit 2c9d81b

@sfackler
Copy link
Member Author

sfackler commented Mar 9, 2015

@bors r=alexcrichton e2605b4

@bors
Copy link
Collaborator

bors commented Mar 9, 2015

⌛ Testing commit e2605b4 with merge 638832e...

@bors bors merged commit e2605b4 into rust-lang:master Mar 9, 2015
Manishearth added a commit to Manishearth/rust that referenced this pull request Mar 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.