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

Add expect block to skip tests #3596

Merged
merged 11 commits into from
Aug 26, 2023
Merged

Add expect block to skip tests #3596

merged 11 commits into from
Aug 26, 2023

Conversation

sksamuel
Copy link
Member

Closes #3582

This adds the ability to do expect { someboolean() } in a test block, which, if false, marks the test as skipped.

Happy to rename "expect" to something else:

  • require(s) clashes with the kotlin sdk method
  • assume clashes with property test assumptions.

@sksamuel sksamuel requested a review from Kantis July 29, 2023 14:49
@Kantis
Copy link
Member

Kantis commented Aug 1, 2023

I like it. Tried to come up with some naming alternatives but it's hard.. @mickeelm you're pretty good at spitting out names, any thoughts on this one? 😄

@Kantis
Copy link
Member

Kantis commented Aug 1, 2023

Btw would this be compatible with having a second variant?:

inline fun <reified T> TestScope.expect(thunk: () -> T) = runCatching {
   thunk()
}.mapError {
   ExpectFailureException
}.getOrThrow()

val user = expect { createUser() } 

@sksamuel
Copy link
Member Author

sksamuel commented Aug 1, 2023

Yeah that's better.

@Kantis
Copy link
Member

Kantis commented Aug 2, 2023

  • assumeSuccess {}
  • assumeSuccessful {}
  • expectSuccess {}

Might make the intent a bit more obvious?

@sksamuel
Copy link
Member Author

sksamuel commented Aug 2, 2023 via email

@mickeelm
Copy link
Contributor

mickeelm commented Aug 5, 2023

I'll try to live up to that introduction :)

I like expect, but I think that a potential problem with expect is the notion of expect a to be b, i.e. all in all the same as shouldBe (or assert) and most importantly, you're likely to expect the test to throw, not to be ignored. There might be a confusion risk here.

I think precondition/condition/prereq are closest to hinting that a false evaluation will result in an ignored test rather than a failed test.

Some other suggestions:

runIf {}
runOnlyIf {}
onlyIf {}
conditional {}
conditionalOn {}
precheck {}

@Kantis
Copy link
Member

Kantis commented Aug 8, 2023

I think I prefer prereq, but I dont feel very strongly about it.

@sksamuel
Copy link
Member Author

I like onlyIf or runIf

@sksamuel
Copy link
Member Author

I can't get two variants to work, it always wants to resolve to the Boolean one. So we might need another name for the one that returns a value.

@sksamuel sksamuel enabled auto-merge (squash) August 26, 2023 16:47
@sksamuel sksamuel disabled auto-merge August 26, 2023 23:20
@sksamuel sksamuel merged commit de72d7c into master Aug 26, 2023
11 checks passed
@sksamuel sksamuel deleted the sks/expects branch August 26, 2023 23:20
*/
object ExpectFailureException : Exception()

fun TestScope.runIf(thunk: () -> Boolean) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could make this runIf(condition: Boolean), then we can have rhe one taking a function return T

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.

Pleasant mechanism for ignoring/skipping a test from within the test code.
3 participants