Errata: testCreateCourseFromValidDict() is severely flawed


The implementation of the testCreateCourseFromValidDictionary() method as a major flaw in that it crashes when parsing fails.

A test must never crash but just produce assertion-failures or -passes!

The problem is that

XCTAssertNonNil(course) will fail correctly if the course could not be parsed but execution of the test will continue.
The next line will then execute with course == nil and hence crash.

A sane implementation of this test would therefore go along the lines of this:

[code]func testCreateCourseFromValidDictionary() {
guard let course = fetcher.courseFromDictionary(Constants.validCourseDict) else {
XCTFail(“Parsing failed”)

XCTAssertEqual(course.title, Constants.title)
XCTAssertEqual(course.url, Constants.url)



If this test fails because it cannot parse the data then it would be a failure of the programmer to create a valid test, not of the test itself. The purpose of the test is show that when given valid data, a course object is created with all the correct values. That is why the test is called “testCreateCourseFromValidDict()”. If you want to test the handling of invalid data that would be a separate test function or set of functions.


I agree with your reasoning, but my argument is still valid.
My test does fulfill exactly the purpose you mentioned, it does however not test the handling of invalid data as you claim. It merely exits in a graceful way so that other tests may run. Imagine you have hundreds of other tests and run this on CI on some server. You will be happily notified of your programming error (as you rightfully called it) yet not crash the process.