A List of XCTest Assertions

All unit test methods use Assertions to validate the result produced by the System Under the Test and present the user with a test method result. It is the assertion that will make a decision whether the test method is passing or failing. This blog post contains a list of assertions you can use with the Xcode Test framework.

XCTAssertion Example

The below code snippet example demonstrates how an assertion can be used in a unit test method. The assertion used in the example is called XCTAssertTrue and the test method will pass if the value it accepts as a parameter validates to boolean true. Otherwise, the test method will fail and a description that is provided as a second parameter will be displayed.

XCTAssertTrue Example

XCTest Assertions

Below is a list of different assertions that can be used in the Xcode Test framework.

Unconditional Fail

  • XCTFail.  XCTFail(“optional description) – Generates a failure immediately and unconditionally,

Equality Tests

Use these to assert a relationship between two items.

  • XCTAssertEqual. XCTAssertEqual(expression1, expression2, “optional description”) – Asserts that two expressions have the same value,
  • XCTAssertEqualWithAccuracy. XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, “optional description”) – Asserts that two expressions have the same value within a certain accuracy,
  • XCTAssertNotEqual. XCTAssertNotEqual(expression1, expression2, “optional description”) – Asserts that two expressions do not have the same value,
  • XCTAssertGreaterThan. XCTAssertGreaterThan(expression1, expression2, “optional description”) – Asserts that the value of one expression is greater than another,
  • XCTAssertLessThan. XCTAssertLessThan(expression1, expression2, “optional description”) – Asserts that one value is less than another,
  • XCTAssertLessThanOrEqual. XCTAssertLessThanOrEqual(expression1, expression2, “optional description”) – Asserts that one value is less than or equal to another.

Boolean Tests

Use these to assert that a Boolean expression evaluates a certain way.

  • XCTAssertTrue. XCTAssertTrue(expression, “optional description”) – Asserts that an expression is true,
  • XCTAssertFalse. XCTAssertFalse(expression, “optional description”) – Asserts that an expression is false.

Nil Tests

Use these to assert that an item is or is not nil.

  • XCTAssertNil. XCTAssertNil(expression, “optional description”) – Asserts that expression is nil,
  • XCTAssertNotNil. XCTAssertNotNil(expression, “optional description”) – Asserts that expression is not nil,
  • XCTUnwrap. Asserts that an expression is not nil and returns the unwrapped value.

Exception Tests

Use these to assert that evaluating an expression generates an exception or not.

  • XCTAssertThrowsError. XCTAssertThrowsError(expression, “optional description”) – Asserts that an expression throws an error,
  • XCTAssertNoThrow. XCTAssertNoThrow(expression, “optional description”) – Asserts that an expression does not throw an error.

I hope this list of XCTest Assertions will be of some aid to you.

Happy Unit Testing 🙋🏻‍♂️

Leave a Reply

Your email address will not be published. Required fields are marked *