t

快速测试预期

什么是 t?

t 是一个使用闭包和错误的简单测试框架。您可以创建一个包含多个步骤、预期和断言的 suite(测试套件)。预期可以用来 expect(期望)一个或多个断言。

t 可以用在哪里?

t 可以用于在函数内部快速测试,以确保某些东西按预期工作。如果需要,t 也可以用于单元测试。

例子

t.suite(测试套件)

t.suite {
    // Add an expectation that asserting true is true and that 2 is equal to 2
    try t.expect {
        try t.assert(true)
        try t.assert(2, isEqualTo: 2)
    }
    
    // Add an assertion that asserting false is not true
    try t.assert(notTrue: false)
    
    // Add an assertion that "Hello" is not equal to "World"
    try t.assert("Hello", isNotEqualTo: "World")
    
    // Log a message
    t.log("📣 Test Log Message")
    
    // Log a t.error
    t.log(error: t.error(description: "Mock Error"))
    
    // Log any error
    struct SomeError: Error { }
    t.log(error: SomeError())
    
    // Add an assertion to check if a value is nil
    let someValue: String? = nil
    try t.assert(isNil: someValue)
    
    // Add an assertion to check if a value is not nil
    let someOtherValue: String? = "💠"
    try t.assert(isNotNil: someOtherValue)
}

t.expect(期望)

try t.expect {
    let someValue: String? = "Hello"
    try t.assert(isNil: someValue)
}

t.assert(断言)

try t.assert("Hello", isEqualTo: "World")

t.log(日志)

t.log("📣 Test Log Message")

XCTest

断言测试套件为真

XCTAssert(
    t.suite {
        try t.assert(true)
    }
)

断言期望为真

XCTAssertNoThrow(
    try t.expect("true is true and that 2 is equal to 2") {
        try t.assert(true)
        try t.assert(2, isEqualTo: 2)
    }
)

断言为假

XCTAssertThrowsError(
    try t.assert(false)
)