53

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

ChaosSpeeder
  • 3,468
  • 5
  • 30
  • 38
  • 4
    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 Nov 04 '14 at 09:08
  • 1
    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 Nov 08 '14 at 18:19
  • 1
    Seriously considering working directly in VIM. – Aaron Mar 02 '16 at 14:35
  • Same sh.. with XCode 8. – qwerty_so Nov 10 '16 at 09:59
  • Maybe this can fix this problem https://github.com/onmyway133/TestTarget – onmyway133 Nov 30 '16 at 12:45

16 Answers16

56

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.

ChaosSpeeder
  • 3,468
  • 5
  • 30
  • 38
  • 3
    In my version (6.2) its under File -> Project Settings – Tyler Apr 01 '15 at 14:55
  • 7
    Worked for me in Xcode 7. Had to shut Xcode down after deleting derived data, then restart. – Mike Taverne Sep 29 '15 at 00:04
  • Same problem in 7.0.1 fixed by the same method. Thanks – Fogmeister Oct 22 '15 at 09:45
  • "After indexing, all tests are working". This was the key for me. I shut down xcode, cleaned derived data manually, restarted xcode, and just left it open and don't do anything until indexing is complete. If i started using xcode, indexing didn't complete successfully. – Ken Ko Feb 02 '16 at 23:04
  • 1
    I still experience this issue on 7.3.1. And still deleting DerivedData and waiting for indexing to finish fixes the problem. – raven_raven Aug 03 '16 at 11:07
24

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

i.e.

func arrayResponseCall() 

should be:

func testArrayResponseCall()
Antoine
  • 23,526
  • 11
  • 88
  • 94
  • Thank you, but the naming prefix is the requirement for all testing function. look at my example, where the method name is testExample(). The bug is already fixed by apple starting with Xcode 6.3. – ChaosSpeeder Jun 24 '15 at 15:54
  • This fixed it for me. This should be made clearer in documentation. I spent over an hour trying to find this bug! – Sethmr Dec 21 '16 at 00:18
  • It's case sensitive ! I tried around for 30 minutes until i noticed that I wrote "Test.." instead of "test.." – nablahero May 30 '17 at 12:33
13

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.

Reid
  • 1,109
  • 1
  • 12
  • 27
  • 1
    Thank you. Look in my answer. The problem is that after a while the internal derived data files got messed up. You have to delete them after a while to enforce the reprocessing or reindexing. – ChaosSpeeder Mar 20 '15 at 17:07
8

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.

Community
  • 1
  • 1
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
7

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

Stephen Chen
  • 3,027
  • 2
  • 27
  • 39
6

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")
}
Ernest Han
  • 395
  • 1
  • 3
  • 10
  • Any edit to the file works for me. Leaving the `XCTAssert` in the files doesn't automatically make them recognizable by Xcode. – Albert Bori Jun 03 '15 at 19:31
  • funny. I tried that and it didn't work as expected since I already had methods starting with the name 'test'. However, I did have a private method within the test file and by commenting that out the test showed them selfs again in the test navigator. Then I uncommented it and everything was back to normal. bug. – Laser Hawk Dec 11 '15 at 21:38
4

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

ajw
  • 2,568
  • 23
  • 27
3

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

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
  • Thank you, restarting doesn't solved my problems and I have it in other swift classes too. I believe my project setup with several framework kits and extensions are leading to this behavior. – ChaosSpeeder Nov 15 '14 at 08:41
3

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:

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
2

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.

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
2

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

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
2

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

Bug Hunter Zoro
  • 1,743
  • 18
  • 21
1

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.

Ari Braginsky
  • 928
  • 1
  • 11
  • 21
1

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

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
1

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

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
0

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()
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Alcivanio
  • 89
  • 1
  • 3