最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - Why is XCTAssertTrue passing when it should fail - Stack Overflow

programmeradmin2浏览0评论

Started on some unit tests today in Xcode and decided to test my test. Turns out they always pass but not sure why.

The following should fail:

struct MyUnitTest {
    @Test func example() throws {
        XCTAssertTrue("a" == "b")
        XCTAssertEqual("a", "b")
    }
}

But they don't. Anyone know why?

Started on some unit tests today in Xcode and decided to test my test. Turns out they always pass but not sure why.

The following should fail:

struct MyUnitTest {
    @Test func example() throws {
        XCTAssertTrue("a" == "b")
        XCTAssertEqual("a", "b")
    }
}

But they don't. Anyone know why?

Share Improve this question edited 18 hours ago Alexander 63.3k13 gold badges105 silver badges167 bronze badges asked 18 hours ago Rob BonnerRob Bonner 9,4248 gold badges36 silver badges57 bronze badges 3
  • You're mixing Swift testing (@Test) and XCTest, which I'm not sure is possible like this – Alexander Commented 18 hours ago
  • That was it, thank you. I choose the wrong template. – Rob Bonner Commented 18 hours ago
  • Swift Testing is generally preferable these days, you would just write: #expect("a" == "b") instead of the two XCTest assertions you tried. – Alexander Commented 18 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

You're using Swift Testing (as indicated by the @Test and lack of XCTestCase subclass), which doesn't use XCTest assertions. Instead you'd write:

struct MyUnitTest {
    @Test func example() throws {
        #expect("a" == "b")
    }
}

If you don't need any instance variables on the struct, you can even just have the test functions at the top level without the boilerplate of a struct. The need to wrap all the test cases in a class was a limitation of Objective-C and XCTest, which no longer applies.

发布评论

评论列表(0)

  1. 暂无评论