Coder
Coder

Reputation: 1681

XCTest: Passing a function parameter

From XCTest file, I am calling a web service call and had put a wait block using "self.waitForExpectationsWithTimeout" API and to get the service response.

I have few test methods to be executed with this service response. When I store this response in a global variable and access it from other test function, this variable is coming as nil. What needs to be done here? Can I pass it as a function parameter?

let serviceResp :NSDictionary!

func test_One() {
        //let expectation: XCTestExpectation = self.expectationWithDescription("HTTP")

        datamanager.fetchData() //Web service

        self.waitForExpectationsWithTimeout(5, handler: { (error: NSError!) -> Void in

//In 5 seconds, I will get response from service and will be stored in datamanager.response. 

    self.serviceResp = datamanager.response
        })
    }


    func test_Two() {
       //self.serviceResp is coming as nil even after assigning a value to it. 
    }

Thanks

Upvotes: 0

Views: 2751

Answers (1)

litso
litso

Reputation: 322

You cannot pass information between XCTest methods in this way. See the following from the Testing with Xcode Docs (Emphasis mine)

For each class, testing starts by running the class setup method. For each test method, a new instance of the class is allocated and its instance setup method executed. After that it runs the test method, and after that the instance teardown method. This sequence repeats for all the test methods in the class. After the last test method teardown in the class has been run, Xcode executes the class teardown method and moves on to the next class. This sequence repeats until all the test methods in all test classes have been run.

If there is information that all your test need to run, consider putting it in the setup method.

Upvotes: 3

Related Questions