12

I have noticed that when a new XCTestCase class is created with a default setUpWithError() and tearDownWithError() methods, there is no call to super.setUpWithError() or super.tearDownWithError() added anymore... If I am not mistaken, before, when I created a new Unit Test case class(a subclass of XCTestCase), in a default template class, there was a call to super.setUp() and super.tearDown() methods. Do you know why have these calls to super have been removed?

When a subclass of UIViewController is created, the default class template does have a call to super.viewDidLoad() for example. Why does a default template for XCTestCase does not have a call to super any more

InterestedDev
  • 578
  • 7
  • 22
  • XCTest now includes throwing variants of the setUp() and tearDown() instance methods, allowing tests to throw errors in Swift during set up or tear down. Override the setUpWithError() or tearDownWithError() methods instead of setUp() or tearDown(), respectively. If an error is thrown by setUpWithError(), the test method is not executed, and if the error was due to calling an XCTSkip* API, the test is marked as skipped instead of failed. (42069831) – Grenoblois Apr 07 '20 at 16:16
  • 4
    @developper thank you. Yes, I am aware about this. Xcode now adds tearDownWithError() rather than tearDown(). My question was about the absence of a call to super.tearDownWithError() – InterestedDev Apr 07 '20 at 18:47
  • 3
    Does this answer your question? [Xcode 10 and super.tearDown](https://stackoverflow.com/questions/53448538/xcode-10-and-super-teardown) – JAB Aug 24 '20 at 18:32

1 Answers1

1

When a test method runs, both setUpWithError and setUp are called. Thus, if you override setUpWithError and not setUp, and if your test situation already has a setUp implementation in the superclass, the superclass implementation will be called without your having to call super.

(And if your test situation has no setUp implemention in the superclass, there was no need to call super in the first place.)

Thus, the pattern shown by the template is correct.

matt
  • 515,959
  • 87
  • 875
  • 1,141