Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using override init() in an XCtest class?

Tags:

ios

swift

xctest

I'm wondering if there is a way to use unit when XCtesting in order to specify a constant variable that isn't torn down between separate test cases? I realize that generally best practices for unit testing are to keep the tests as self contained as possible but in my current situation it would make the tests execute a lot faster if I were able to do this and keep a constant variable between test cases.

Currently, any type of init function that I call

override init() {
    super.init()
}

Leaves me with an EXC_BAD_INSTRUCTION error. If I can't use init() in XCTestCase, is there another work around that I can use?

like image 203
helloworld345123123123 Avatar asked Oct 21 '15 15:10

helloworld345123123123


People also ask

How do I run XCTest?

To run your app's XCTests on Test Lab devices, build it for testing on a Generic iOS Device: From the device dropdown at the top of your Xcode workspace window, select Generic iOS Device. In the macOS menu bar, select Product > Build For > Testing.

What is XCTest in Xcode?

Overview. Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow. Tests assert that certain conditions are satisfied during code execution, and record test failures (with optional messages) if those conditions aren't satisfied.

How do I turn off XCTest?

You can disable XCTests run by Xcode by right clicking on the test symbol in the editor tray on the left. You'll get this menu, and you can select the "Disable " option.


1 Answers

Try moving the variable outside of the XCTestCase class.

import XCTest

var counter = 0 // Note this is outside the class declaration

class MyTests: XCTestCase {
    override func setUp() {
        super.setUp()
        counter++
        print("Counter: \(counter)")
    }

    func testOne() {
        ...
    }

    func testTwo() {
        ...
    }

    func testThree() {
        ...
    }
}

This gives an output like this.

...
Counter: 1
...
Counter: 2
...
Counter: 3
...
like image 59
Mike Cole Avatar answered Sep 23 '22 01:09

Mike Cole