In einem der alten Projekte wurden Behauptungen von JUnit, kotlin.test und AssertJ angehÀuft. Dies war nicht sein einziges Problem: Es wurde im Allgemeinen als Brief von Onkel Fjodor geschrieben, und es blieb keine Zeit, anzuhalten und es in eine einzige Form zu bringen. Und jetzt ist es soweit.
Der Artikel enthĂ€lt eine Mini-Recherche darĂŒber, welche Behauptungen nach subjektiven Kriterien besser sind. Zuerst wollte ich etwas Einfaches tun: eine Reihe von Tests durchfĂŒhren, um Optionen schnell mit Copy-Paste zu vernieten. Dann hob er die allgemeinen Testdaten hervor, automatisierte einige der ĂberprĂŒfungen und wie alles lief ... Das Ergebnis war ein kleiner Rosetta-Stein, und dieser Artikel kann fĂŒr Sie nĂŒtzlich sein, um eine Bibliothek von Asserts auszuwĂ€hlen, die Ihren RealitĂ€ten entspricht.
Ich werde sofort reservieren, dass der Artikel keine Test-Frameworks, Test-AnsĂ€tze und einige knifflige AnsĂ€tze zur Datenvalidierung vergleicht. Wir werden ĂŒber einfache Behauptungen sprechen.
Wenn Sie zu faul sind, um langweilige Argumente, die Geschichte meiner PrĂŒfungen und andere Details zu lesen, können Sie direkt zu den Vergleichsergebnissen gehen .
Ein bisschen Hintergrund
Scala, â ScalaTest. , - , - .
Kotlin, - , , Kotest ( , ).
, , â . :
- Kotlin IntelliJ Idea. Scala- â . , ScalaTest , . IntelliJ
<Click to see difference>
, . , , IntelliJ Idea â Kotlin- - , ?
- .
1 != 2
, , "expected" "actual". " 100 , , ", , "⊠, , , ". , ? , â , , - . - .
assertEquals(expected, actual)
â , . , â /, " , , " ,contains
,includes
. â , . - . -
assertThat("Friendship").contains("end")
. - . - JUnit4, ,
ExpectedException
@Rule
. - () .
- .
- . â , . , : , , generic-, . -:
assertThat(generic<Boolean>(input)).isEqualTo(true)
.<Boolean>
. . - , . . ? â , , . - , equals. - .
, , , , , . , â , . , -, QA-.
, 5 , , â .
:
- ScalaTest â .
- JUnit 5 â Java-.
- kotlin.test â multiplatform . â JUnit, .
- AssertJ â . FestAssert, - .
- Kotest â KotlinTest, kotlin.test. , ScalaTest. 1-22 â scala.
- Truth â . , AssertJ.
- Hamrest Ââ . valid4j.
- Strikt â AssertJ .
- Kluent â , JUnit ( â kotlin.test), Kotest. â , .
- Atrium â , AssertJ, . â ( maven/gradle).
- Expekt â Chai.js. : â 4 .
- AssertK â AssertJ, AssertK ( ).
- HamKrest â Hamrest, HamKrest ( Hamcrest ).
â , -.
, , 1 â 1 . , , - , " , " "".
, - , JUnit , , , , , . ScalaTest, : , , â . : ? , :). / , , .
: listOf(1,2,3)
? â -, â . , , : , . , .
type erasure. Reified inline-, .
assertThrows<T>{...}
, reified :
assertThrows(expectedClass){...}
. , kotlin.test Asserter: , . , ?:)
GitHub. ScalaTest , .
: 0 â , 0.5 â , 1 â . â 9 .
-, , . . , . , - , " " , " " . .
Kotest | ± | ± | + | + | + | + | + | - | 6.0 | |
Kluent | ± | ± | + | + | + | + | + | - | 6.0 | |
AssertJ | ± | + | ± | + | ± | + | + | ± | 6.0 | |
Truth | ± | + | + | + | - | + | + | - | 5.5 | |
Strikt | ± | ± | ± | + | + | + | + | - | 5.5 | |
ScalaTest | ± | ± | ± | + | + | + | + | - | 5.5 | |
HamKrest | ± | - | ± | + | + | ± | + | - | 5.5 | |
AssertK | ± | ± | ± | + | ± | + | + | - | 5.0 | |
Atrium | ± | ± | ± | + | + | ± | + | - | 5.0 | |
Hamrest | ± | ± | ± | + | - | ± | + | - | 5.0 | |
JUnit | + | + | - | ± | + | - | ± | - | 4.5 | |
kotlin.test | + | ± | - | - | + | - | - | - | 3.5 | |
Expekt | ± | ± | - | + | - | ± | + | - | 3.5 |
:
-
"hello".shouldBeEqualTo("hello")
,"hello" `should be equal to` "hello"
. DSL . - :
invoking { block() } shouldThrow expectedClass.kotlin withMessage expectedMessage
- , , .
Expected Iterable to contain none of "[1, 3]"
â , Iterable . - :
<Click to see difference>
. - : .
â ⊠,
containsExactly
, âhasSameElementsAs
, â.usingRecursiveComparison().isEqualTo
.
:
<Click to see difference>
.
: - , . , , .
:
.usingRecursiveComparison()
, . : , , . , , ,
assertThat(actual) .usingRecursiveComparison() .isNotEqualTo(unexpected)
: .
: . , DSL.
- , .
- : , ,
assertThrows
JUnit5. , JUnit , ? - : , , :
containsAtLeastElementsIn
. ,assertThat(actual).isEqualTo(expected)
. - :
<Click to see difference>
. - : .
- : , .
- " ":
expected: 1 but was : 2 at asserts.truth.TruthAsserts.simpleAssert(TruthAsserts.kt:10) at common.FailedAssertsTestBase.simple assert should have descriptive message(FailedAssertsTestBase.kt:20) at [[Reflective call: 4 frames collapsed (https://goo.gl/aH3UyP)]].(:0) at [[Testing framework: 27 frames collapsed (https://goo.gl/aH3UyP)]].(:0) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at [[Testing framework: 9 frames collapsed (https://goo.gl/aH3UyP)]].(:0) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at [[Testing framework: 9 frames collapsed (https://goo.gl/aH3UyP)]].(:0) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at [[Testing framework: 17 frames collapsed (https://goo.gl/aH3UyP)]].(:0) at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:99) ...
, reified, : .
:
expectThat(haystack).not().contains(needle)
,expectThat(collection).doesNotContain(items)
.
:
contentEquals
. :expectThat(actual).not().contentEquals(unexpected)
. , ,Array<T>
Strikt - . âcontainsExactly
, âcontainsExactlyInAnyOrder
.
: . , . :
val actual: Array<String> = arrayOf("1") val expected: Array<String> = arrayOf("2") expectThat(actual).contentEquals(expected)
,
contentEquals
. ,contentEquals
:
infix fun <T> Assertion.Builder<Array<out T>>.contentEquals(other: Array<out T>)
-
val actual: Array<out String> = arrayOf("1") val expected: Array<String> = arrayOf("2") expectThat(actual).contentEquals(expected)
:
<Click to see difference>
.
: , .
: .
- , , . â , .
- , Hamcrest, - : -.
- â :
assertThat( { block() }, throws(has(RuntimeException::message, equalTo(expectedMessage))))
- : . - 3,5 . :
assertThat(collection, allOf(items.map { hasElement(it) }))
. - .
- : .
- :
<Click to see difference>
. - â - :
expected: a value that not contains 1 or contains 3 but contains 1 or contains 3
â AssertJ. ( , -).
: AssertJ
assertThat(collection).containsAll(items)
, AssertK ,containsAll
vararg
. ,containsAll(1,2,3)
, . , , â . â . ,containsOnly
containsExactly
.
:
<Click to see difference>
.
: - , , .
:
.usingRecursiveComparison()
.
â ( ), :
expected to contain exactly:<[3, 4, 5]> but was:<[1, 2, 3]> at index:0 unexpected:<1> at index:1 unexpected:<2> at index:1 expected:<4> at index:2 expected:<5>
?
: .
- : fluent infix.
assertThat(x).isEqualTo(y)
x shouldBe y
, ,expect(x).toBe(y)
expect(x) toBe y
. , , "". -o
:expect(x) contains o atLeast 1 butAtMost 2 value "hello"
. , , . infix- ( - ), Atrium fluent-. - : :
notToBe
,containsNot
. . , :contains
vararg
,containsElementsOf
, . ,contains(1,2,3)
, .expect(collection).containsNot.elementsOf(items)
. - ,
toList
. - , reified, : .
- : .
- :
<Click to see difference>
. - : ( , ), :
expected that subject: [4, 2, 1] (java.util.Arrays.ArrayList <938196491>) ââdoes not contain: ⏠an entry which is: 1 (kotlin.Int <211381230>) ââânumber of such entries: 1 âis: 0 (kotlin.Int <1934798916>) ââhas at least one element: true âis: true
- : .
: (
assertThat(actual, `is`(not(unexpected)))
assertThat(actual, not(unexpected))
containsString
vscontains
vshasItem
vshasItems
. , :hasItems
vararg
,Set<T>
T
. ,hasItems(1,2,3)
, .
assertThat(collection, allOf(items.map { hasItem(it) }))
:
assertThat(collection, not(anyOf(items.map { hasItem(it) })))
hasItems
, ± "", , .
: .
:
<Click to see difference>
.
: .
: .
- : -
assertEquals(expected, actual)
, :assertArrayEquals
,assertIterableEquals
.. - : , JUnit - , .
- :
assertLinesMatch(listOf(".*$needle.*"), listOf(haystack))
, . - :
assertLinesMatch
, ,assertIterableEquals
. - : ,
assertIterableEquals
Map
Set
, . - : .
- . JUnit, . , .
- , JUnit, :
- .
-
assertIterableEquals
, . - : JUnit'
assertEquals
, kotlin.test , . - : .
Kotest, Kluent AssertJ. , Kotlin , AssertJ ( ). , .
â , , AssertJ. , , .