sde icon indicating copy to clipboard operation
sde copied to clipboard

Debug Unit Test

Open TofPlay opened this issue 8 years ago • 7 comments

For unit test we launch

$ swift test

Now I want to debug (put breakpoints and check variables) the units tests how I have to set VSCode?

TofPlay avatar Feb 09 '17 07:02 TofPlay

I am back:) The debugger now works for debugging an executable. You set the executable in the launch file and (SDE adds sources file infos for you in (swift-)lldb), so you can stop when run. The whole work is just the UI remapping for (swift-)lldb work flow.

I have not examined the case of swift test. And further, the debug side are not well tested(there may be bugs around). I will feedback some idea after investigation for me.

jinmingjian avatar Feb 11 '17 01:02 jinmingjian

Any news?

TofPlay avatar Feb 15 '17 17:02 TofPlay

@TofPlay sorry. Recently, I am back to some big data thing and job hunting:)

In fact, I find nothing new for "swift test".

for example, we use official example repo: https://github.com/apple/example-package-fisheryates

when first run "swift test" in shell, we have

swift test                                                             1 ↵
Compile Swift Module 'FisherYatesPackageTests' (1 sources)
Linking ./.build/debug/FisherYatesPackageTests.xctest
Test Suite 'All tests' started at 10:36:19.783
Test Suite 'debug.xctest' started at 10:36:19.785
Test Suite 'FisherYatesShuffleTest' started at 10:36:19.785
Test Case 'FisherYatesShuffleTest.testShuffle' started at 10:36:19.785
Test Case 'FisherYatesShuffleTest.testShuffle' passed (0.015 seconds)
Test Suite 'FisherYatesShuffleTest' passed at 10:36:19.801
	 Executed 1 test, with 0 failures (0 unexpected) in 0.015 (0.015) seconds
Test Suite 'debug.xctest' passed at 10:36:19.801
	 Executed 1 test, with 0 failures (0 unexpected) in 0.015 (0.015) seconds
Test Suite 'All tests' passed at 10:36:19.801
	 Executed 1 test, with 0 failures (0 unexpected) in 0.015 (0.015) seconds

note: as from output, we know the executable linked to ./.build/debug/FisherYatesPackageTests.xctest, then config launch.json in vscode like this:

// Use IntelliSense to learn about possible Mock debug attributes.
// Hover to view descriptions of existing attributes.
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "swift-debug",
            "request": "launch",
            "name": "Swift Program Debug",
            "program": "${workspaceRoot}/.build/debug/FisherYatesPackageTests.xctest"
        }
    ]
}

set a breakpoint in the source like FisherYatesShuffleTest.swift here.

click "Start Debugging" button, then we get this:

xctest_debug

note: the silly display <could not... assumed to be a bug of swift's lldb in Linux as I mentioned in README. it seems system header file loading problem. The workaround is to use log helper. I admit this bug makes debugging largely broken. But... :frowning:

jinmingjian avatar Feb 16 '17 02:02 jinmingjian

note: change one line in test drvier LinuxMain.swift => @testable import FisherYatesTests to make the "swift test" passed. This seem an obvious bug of example for swift 3.1.

jinmingjian avatar Feb 16 '17 02:02 jinmingjian

@jinmingjian when I try to debug it does not work and I have the following message in the window "OUTPUT"

[Error - 9:13:50 AM] Request textDocument/hover failed.
  Message: Request textDocument/hover failed with message: Cannot read property 'Symbol(Symbol.iterator)' of undefined
  Code: -32603 

Update: If I put a break point on LinuxMain.swift it's work. It's when I put a break point on a test function it doesn't work and I have the message above.

TofPlay avatar Feb 16 '17 08:02 TofPlay

the output may say the language serve has problem. But the debugging functionalities are only related to the debug server other than the language server. May the debug server crash?

Can you show your some codes for reproducing?

jinmingjian avatar Feb 16 '17 08:02 jinmingjian

I can't give you some codes of my project. The project is closed source but is really basic.

Here a pseudo environment identical to mine. I hope this will help:

Environment:

  • Ubuntu 16.10
  • Swift version 3.1-dev

Run swift build with no error

Folders:

$ tree
.
├── Package.swift
├── Sources
│   ├── SomeSource1.swift
│   └── SomeSourceN.swift
└── Tests
    ├── MyFrameworkTests
    │   ├── MyFrameworkBasicTests.swift
    │   └── MyFrameworkAdvancedTests.swift
    └── LinuxMain.swift

launch.json for VSCode:

// Use IntelliSense to learn about possible Mock debug attributes.
// Hover to view descriptions of existing attributes.
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "swift-debug",
            "request": "launch",
            "name": "Swift Program Debug",
            "program": "${workspaceRoot}/.build/debug/MyFrameworkPackageTests.xctest"
        }
    ]
}

LinuxMain.swift:

import XCTest
@testable import MyFrameworkTests

// Break point work here
XCTMain([
  testCase(MyFrameworkBasicTests.allTests),
])

MyFrameworkBasicTests.swift:

import XCTest
@testable import MyFramework

class MyFrameworkBasicTests: XCTestCase {
  
  static var allTests : [(String, (MyFrameworkBasicTests) -> () throws -> Void)] {
    // Break point work here
    return [
      ("test_01", test_01),
      ("test_02", test_02)
    ]
  }

  func test_01() {
    // Break point doesn't work here
    // Some codes
  }
  
  func test_02() {
    // Break point doesn't work here
    // Some codes
  }
}  

TofPlay avatar Feb 16 '17 09:02 TofPlay