在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):willowtreeapps/assertk开源软件地址(OpenSource Url):https://github.com/willowtreeapps/assertk开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):assertkassertk is a fluent assertion library for Kotlin inspired by AssertJ. Why another assertion library?You might be asking, "If AssertJ already exists, why create another library?". It's true, assertk is very similar to AssertJ. But assertk is written in Kotlin so it has one major advantage: extension methods. This makes adding your own assertion methods far simpler. See Custom Assertions below to find out how to do this. SetupGradle/JVMrepositories {
mavenCentral()
}
dependencies {
testImplementation 'com.willowtreeapps.assertk:assertk-jvm:0.25'
} Javascript/CommonReplace dependency on UsageSimple usage is to wrap the value or property you are testing in import assertk.assertThat
import assertk.assertions.*
class PersonTest {
val person = Person(name = "Bob", age = 18)
@Test
fun testName() {
assertThat(person.name).isEqualTo("Alice")
// -> expected:<["Alice"]> but was:<["Bob"]>
}
@Test
fun testAge() {
assertThat(person.age, "age").isGreaterThan(20)
// -> expected [age] to be greater than:<20> but was:<18>
}
@Test
fun testNameProperty() {
assertThat(person::name).isEqualTo("Alice")
// -> expected [name]:<["Alice"]> but was:<["Bob"]>
}
} You can see all built-in assertions in the docs. NullabilitySince null is a first-class concept in kotlin's type system, you need to be explicit in your assertions. val nullString: String? = null
assertThat(nullString).hasLength(4) will not compile, since val nullString: String? = null
assertThat(nullString).isNotNull().hasLength(4)
// -> expected to not be null This will first ensure the string is not null before running any other checks. Multiple assertionsYou can assert multiple things on a single value by providing a lambda as the second argument. All assertions will be run even if the first one fails. val string = "Test"
assertThat(string).all {
startsWith("L")
hasLength(3)
}
// -> The following 2 assertions failed:
// - expected to start with:<"L"> but was:<"Test">
// - expected to have length:<3> but was:<"Test"> (4) You can wrap multiple assertions in an assertAll {
assertThat(false).isTrue()
assertThat(true).isFalse()
}
// -> The following 2 assertions failed:
// - expected to be true
// - expected to be false Iterable/List AssertionsYou can assert on the contents of an
Extracting dataThere's a few ways you extract the data you want to assert on. While you can do this yourself before calling the assertion, these methods will add the extra context to the failure message which can be helpful. The simplest way is with val person = Person(age = 22)
assertThat(person).prop(Person::age).isEqualTo(20)
// -> expected [age]:<2[0]> but was:<2[2]> (Person(age=22)) For collections, you can use assertThat(listOf(1, 2, 3)).index(1).isEqualTo(1)
// -> expected: [[1]]:<1> but was:<2> ([1, 2, 3])
assertThat(mapOf("one" to 1, "two" to 2, "three" to 3)).key("two").isEqualTo(1)
// -> expected: [["two"]]:<1> but was:<2> ({"one"=1, "two"=2, "three"=3}) You can also extract a property from a collection using val people = listOf(Person(name = "Sue"), Person(name = "Bob"))
assertThat(people)
.extracting(Person::name)
.containsExactly("Sue", "Bob") ExceptionsIf you expect an exception to be thrown, you can use the version of assertThat {
throw Exception("error")
}.isFailure().hasMessage("wrong")
// -> expected [message] to be:<["wrong"]> but was:<["error"]> This method also allows you to assert on successfully returned values. assertThat { 1 + 1 }.isSuccess().isNegative()
// -> expected to be negative but was:<2> Table AssertionsIf you have multiple sets of values you want to test with, you can create a table assertion. tableOf("a", "b", "result")
.row(0, 0, 1)
.row(1, 2, 4)
.forAll { a, b, result ->
assertThat(a + b).isEqualTo(result)
}
// -> the following 2 assertions failed:
// on row:(a=<0>,b=<0>,result=<1>)
// - expected:<[1]> but was:<[0]>
// on row:(a=<1>,b=<2>,result=<4>)
// - expected:<[4]> but was:<[3]> Up to 4 columns are supported. Custom AssertionsOne of the goals of this library is to make custom assertions easy to make. All assertions are just extension methods. fun Assert<Person>.hasAge(expected: Int) {
prop(Person::age).isEqualTo(expected)
}
assertThat(person).hasAge(20)
// -> expected [age]:<2[0]> but was:<2[2]> (Person(age=22)) For completely custom assertions, you have a few building blocks. fun Assert<Person>.hasAge(expected: Int) = given { actual ->
if (actual.age == expected) return
expected("age:${show(expected)} but was age:${show(actual.age)}")
}
assertThat(person).hasAge(20)
// -> expected age:<20> but was age:<22> You can also build assertions that chain by using fun Assert<Person>.hasMiddleName(): Assert<String> = transform(appendName("middleName", seperator = ".")) { actual ->
if (actual.middleName != null) {
actual.middleName
} else {
expected("to not be null")
}
}
assertThat(person).hasMiddleName().isEqualTo("Lorie")
// -> expected [middleName]:to not be null Note: this is a bit of a contrived example as you'd probably want to build this out of existing assertions instead. fun Assert<Person>.hasMiddleName(): Assert<String> = prop(Person::middleName).isNotNull() The general rule of thumb is to prefer building out of the existing assertions unless you can give a more meaningful error message. Contributing to assertkContributions are more than welcome! Please see the Contributing Guidelines and be mindful of our Code of Conduct. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论