53
votes

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

16
I have the same issue. No solution yet. In the test navigator I can see the icons to run individual tests. But if I run an individual test by clicking on such an icon, all tests are executed instead of only the one I clicked. - Peter Fennema
the more my project is growing, the more worse it is getting. I have no IntelliSense in many swift files. I hope, the bugs will be fixed with further versions of Xcode. - ChaosSpeeder
Seriously considering working directly in VIM. - Aaron
Same sh.. with XCode 8. - qwerty_so
Maybe this can fix this problem github.com/onmyway133/TestTarget - onmyway133

16 Answers

56
votes

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.

24
votes

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

i.e.

func arrayResponseCall() 

should be:

func testArrayResponseCall()
13
votes

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.

8
votes

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.

7
votes

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

6
votes

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")
}
4
votes

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!!.

3
votes

(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.

3
votes

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:

2
votes

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.

2
votes

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

2
votes

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

1
votes

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.

1
votes

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

1
votes

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

0
votes

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()