Kotest: JUnit Style Method/Function Spec

Created on 28 Jun 2016  路  11Comments  路  Source: kotest/kotest

Consider adding a junit style "function per test" spec for people who don't want to nest things in init blocks.

class MethodSpecTest : MethodSpec() {
  fun testone() {
  }
  fun someOtherTest() { 
  }
}
question

All 11 comments

@helmbold any thoughts?

That would be great.... but IMHO it would be better have some way to choose what methods are tests and whats are not: prefix, annotations ....

I guess the standard "test" prefix makes sense and allow people to override what prefix to use by overriding a val. And then on top of that could support annotations too.

@sksamuel : your option seams the best way to follow.

I'm not a fan of test as a prefix for test methods. In my experience this almost always leads to poor test names! Using should leads to much more descriptive names ("testBookingAfterClosingTime" vs. "shouldThrowExceptionWhenBookingAfterClosingTime"). But should is not a keyword in JUnit. The way JUnit marks test is @Test (almost nobody is using old JUnit versions without annotytions anymore) and everything else is not familiar to JUnit users. And the whole point of this suggestion is, to offer something familiar for JUnit users. So the only reasonable option would be to introduce @Test. However annotations are more or less a workaround and in many cases not necessary with the possibilities of modern languages like Kotlin or Scala. Annotations would probably be in conflict with several other things of KotlinTest (like config).

Another problem I see here, is that method based style would introduce a complete new way to write tests (not in an init block).

To sum up: I think it is simply not worth to add such a testing style. It could do more harm than good. If someone wants to stay with JUnit style, he can just use JUnit with Kotlin.

I can see your point that there's no point remaking junit - just use junit. But it's not a massive amount of work to support fun shouldDoSomething() and might be a gateway drug?

ScalaTests recommends FunSpec for JUnit users. I was used to JUnit and TestNG style before coming to Scala and ScalaTest, and it was really easy to pick up the new syntax.

The new syntax would be a bit like a framework in a framework. Test methods wouldn't be written in an init block, a test case would no longer be configured with a config method. And let's take a concrete example: ignoring tests. Should this be done with an @Config(...) annotation? Or should there a @Ignore annoation like in JUnit?

I think it is not that hard to pick up a new syntax instead of @Test.

Closed :)

Glad to read that :-)

Ok ... I just thought that it could be useful to have the possibility to play with test that don't run in init code, but maybe that way can lead to better and less coupled tests.

I'm coming from python and I'm just happy to try a new way to write tests. At this time WordSpec is my best choice and I love it.

I do agree with you a bit @la10736 hence the ticket. But I think @helmbold has a convincing argument. As for WordSpec - that's always my first choice in ScalaTest, which is my day to day language. It's very nice.

Was this page helpful?
0 / 5 - 0 ratings