Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Unit Testing: XCTestSuite, XCTest, XCTestRun, XCTestCase methods

In my daily unit test coding with Xcode, I only use XCTestCase. There are also these other classes that don't seem to get used much such as: XCTestSuite, XCTest, XCTestRun.

What are XCTestSuite, XCTest, XCTestRun for? When do you use them?

Also, XCTestCase header has a few methods such as:

defaultTestSuite
invokeTest
testCaseWithInvocation:
testCaseWithSelector:

How and when to use the above?

I am having trouble finding documentation on the above XCTest-classes and methods.

like image 846
Boon Avatar asked Oct 01 '22 19:10

Boon


1 Answers

Well, this question is pretty good and I just wondering why this question is being ignored.

As the document saying:

XCTestCase is a concrete subclass of XCTest that should be the override point for most developers creating tests for their projects. A test case subclass can have multiple test methods and supports setup and tear down that executes for every test method as well as class level setup and tear down.

On the other hand, this is what XCTestSuite defined:

A concrete subclass of XCTest, XCTestSuite is a collection of test cases. Alternatively, a test suite can extract the tests to be run automatically.

Well, with XCTestSuite, you can construct your own test suite for specific subset of test cases, instead of the default suite ( [XCTestCase defaultTestSuite] ), which as all test cases.

Actually, the default XCTestSuite is composed of every test case found in the runtime environment - all methods with no parameters, returning no value, and prefixed with ‘test’ in all subclasses of XCTestCase.

What about the XCTestRun class?

A test run collects information about the execution of a test. Failures in explicit test assertions are classified as "expected", while failures from unrelated or uncaught exceptions are classified as "unexpected".

With XCTestRun, you can record information likes startDate, totalDuration, failureCount etc when the test is starting, or somethings like hasSucceeded when done, and therefore you got the result of running a test. XCTestRun gives you controlability to focus what is happening or happened about the test.

Back to XCTestCase, you will notice that there are methods named testCaseWithInvocation: and testCaseWithSelector: if you read source code. And I recommend you to do for more digging.

How do they work together ?

I've found that there is an awesome explanation in Quick's QuickSpec source file.

XCTest automatically compiles a list of XCTestCase subclasses included in the test target. It iterates over each class in that list, and creates a new instance of that class for each test method. It then creates an "invocation" to execute that test method. The invocation is an instance of NSInvocation, which represents a single message send in Objective-C. The invocation is set on the XCTestCase instance, and the test is run.

Some links:

http://modocache.io/probing-sentestingkit https://github.com/Quick/Quick/blob/master/Sources/Quick/QuickSpec.swift https://developer.apple.com/reference/xctest/xctest?language=objc

like image 128
Zigii Wong Avatar answered Oct 13 '22 11:10

Zigii Wong