ChaosSpeeder
ChaosSpeeder

Reputation: 3508

Xcode 6 Editor doesn't recognize unit tests

I have a problem that the Xcode IDE 6 doesn't detect my swift unit test cases correctly. When I run the unit tests, all tests were executed.

But in the IDE while editing the unit tests aren't recognized. I have to run the whole unit test suite in order to run a single test.

I couldn't figure out how I avoid this glitch.

enter image description here

Upvotes: 53

Views: 13705

Answers (16)

Bug Hunter Zoro
Bug Hunter Zoro

Reputation: 1915

In my case I had a reference to an old test target which was causing the issue, so I got rid of the missing target and clean the solution and then my test cases were working fine.

Xcode test Schemes

Also make sure that the test methods you create should always start with the keyword "test" else Xcode won't be able to identify the test methods an example of the same is

func test_getPhotos_With_Good_Request_Returns_PhotoCollection(){

    //ARRANGE

    //ACT

   //ASSERT

}

Hope it helps

Upvotes: 2

ScottyBlades
ScottyBlades

Reputation: 13993

Here is what worked for me. Even though the unit test says it has 0 tests available, I clicked play, and then it ran through all 4 tests and then it showed.

enter image description here

Upvotes: 1

Alcivanio
Alcivanio

Reputation: 89

I solved my problem just turning the private methods into public.

So, if this is the same problema you're facing, switch:

private func testString()

to:

public func testString()

Upvotes: 0

Stephen Chen
Stephen Chen

Reputation: 3057

For me , I need to add Tests to below here enter image description here

Upvotes: 7

Mike Taverne
Mike Taverne

Reputation: 9352

When you add a new test, save the file (Cmd+S) and the diamond will appear (verified on Xcode 7.3)

Upvotes: 1

ajw
ajw

Reputation: 2702

Make sure the test case name begins with "test" followed by any name you want and build(cmd+B) the project.the diamond will appear!!.

Upvotes: 4

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

For me it was enough to select Product->Test and wait a bit. When the tests just start running all the diamonds become available

Upvotes: 2

Jayprakash Dubey
Jayprakash Dubey

Reputation: 36447

Below are few solution for this issue :

  1. Wait for sometime. It takes time to load diamond sometimes. Navigate between different files and then move to same test-case it should appear.

  2. Clean project, Clean build folder and even delete derived data content. Check this how to delete derived data safely.

  3. Quit Xcode and open it again.

  4. Make sure that your test-case name begins with testFunc_Name

  5. Sometimes your test-case file may contain function other than test-case. In such scenario diamond symbol doesn't appears. Remove such function.

In my case 1, 3 and 5 solution often worked for me.

Upvotes: 9

Suragch
Suragch

Reputation: 511736

Xcode 7

My full answer is here.

In Xcode 7 getting Unit Testing set up is a little easier than in Xcode 6 (no need to annotate class and methods as public). Use @testable before the import of the class name.

import XCTest
@testable import MyProject

class MyClassTests: XCTestCase {

    func testMyMethod() {
        
        let myClass = MyClass()
        let sum = myClass.addTwoNumbers(1, 2)
        
        XCTAssertEqual(sum, 3)
    }
}

In your class you don't have to do anything special.

class MyClass {

    func addTwoNumbers(a: Int, b: Int) -> Int {
        return a + b
    }
}

It is possible you may also have to set "Defines Module" under Packaging to YES for your app's Build Settings.

See also these answers:

Upvotes: 3

Ari Braginsky
Ari Braginsky

Reputation: 926

Sometimes all you need to do is wait for Xcode to finish indexing all your files. For large projects my unit test navigator view is frequently empty until it finishes.

Upvotes: 1

Antoine
Antoine

Reputation: 23976

For me the fix was to prefix all method with 'test'

i.e.

func arrayResponseCall() 

should be:

func testArrayResponseCall()

Upvotes: 24

Ernest Han
Ernest Han

Reputation: 434

I had the same problem too. Just make sure each of your test cases has some sort of XCTAssert() Statements.

func testSomething(){
     XCTAssert(true, "Pass")
}

Upvotes: 6

ChaosSpeeder
ChaosSpeeder

Reputation: 3508

Problem is solved. All I have to do is to launch the "Window -> Projects" window and delete the "Derived Data. After indexing all tests are working.

In the meanwhile apple is fixing the bugs in the Xcode 6.3 editor bit by bit.

Upvotes: 56

Reid
Reid

Reputation: 1129

The "fix" for me was to add a new test. I made up some nonsense:

func testThatNothing() {
    XCTAssertTrue(true, "True should be true")
}

When I ran the tests again, all the tests in that file were recognized by the editor. I deleted the bogus test and it's all still fine. Unfortunately I had to do this in each file, but at least it works. Hope this helps someone.

Upvotes: 14

Olivia Stork
Olivia Stork

Reputation: 4809

I had a similar error (though in Objective C, not swift). If I added a new test method to the class, the new method wouldn't show up in the test navigator nor would it be executed when I ran the whole bundle. I also wouldn't get those dots in the side bar next to each method that, if clicked, would run just one test method.

The only thing fixed my problem was deleting my whole test class (saving its contents elsewhere temporarily) then recreating the test class and (perhaps more carefully?) setting the build settings all over again.

Upvotes: 2

Sophie Alpert
Sophie Alpert

Reputation: 143134

(Since your comment indicates you're still having this problem after a week, this might not help you, but…)

I ran into this problem where adding a new (Swift) test in 6.1 wouldn't make it show up in the "Test Navigator" or the scheme editor -- restarting Xcode fixed the problem and now I can run the tests individually.

Upvotes: 3

Related Questions