vknabel / vscode-swift-development-environment

New home of Swift Development Environment for VS Code
https://marketplace.visualstudio.com/items?itemName=vknabel.vscode-swift-development-environment
Apache License 2.0
175 stars 14 forks source link

Debugging tests #33

Closed yeswolf closed 6 years ago

yeswolf commented 6 years ago

We can debug tests in the following way:

  1. Add
{
  "label": "tests",
  "type": "process",
  "command": "swift",
  "group": "build",
  "args": [
    "build",
    "--build-tests"
  ]
}

to tasks.json

  1. Configure the following task in launch.json:
{
  "type": "lldb",
  "request": "launch",
  "name": "Debug tests",
  "program": "<path to xctest executable>", //For example,  /Applications/Xcode.app/Contents/Developer/usr/bin/xctest
  "args": [
    "${workspaceFolder}.build/debug/<xctest bundle name>.xctest"
  ],
  "preLaunchTask": "tests"
}

Haven't tested it on Linux, but I think that only path to the xctest executable will be different.

vknabel commented 6 years ago

@yeswolf again, thank you very much for your help!

I played around a bit on linux. On mac the generated .xctest is a bundle, on linux .xctest is actually the executable.

I did not test this using vscode, but the correct launch.json for Linux should be:

{
  "type": "lldb",
  "request": "launch",
  "name": "Debug tests",
  "program": "./.build/x86_64-unknown-linux/debug/appPackageTests.xctest",
  "preLaunchTask": "tests"
}

Once both variants have been tested successfully, we can add it to the readme.

vknabel commented 6 years ago

Thank you very much @yeswolf!

ilyapuchka commented 6 years ago

I'm having some troubles here. My goal is to run not just plain swift tests, but XCUITest bundle. I managed to write a task that builds/runs tests fine, but I want to integrate it with SDE debugger to be able to debug them. Here is my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        // Running unit tests
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug tests on macOS",
            "program": "/Applications/Xcode.app/Contents/Developer/usr/bin/xctest",
            "args": [
                "-XCTest",
                "XCTest-Gherkin_Tests/ExampleNativeTests/testNestedSteps",
                "${workspaceFolder}/build/Build/Products/Debug-iphonesimulator/XCTest-Gherkin_Tests.xctest"
            ],
            "preLaunchTask": "Run Selected Gherkin Test"
        }
    ]
}

This fails with error:

The bundle “XCTest-Gherkin_Tests.xctest” couldn’t be loaded because its executable couldn’t be located. Try reinstalling the bundle.

I've also tried to run xcodebuild instead:

{
    "version": "0.2.0",
    "configurations": [
        // Running unit tests
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug tests on macOS",
            "program": "/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild",/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild
            "args": [
                "test-without-building",
                "-xctestrun",
                "${workspaceFolder}/.build/Build/Products/XCTest-Gherkin-Example_iphonesimulator11.4-x86_64.xctestrun"
            ],
            "preLaunchTask": "Run Selected Gherkin Test"
        }
    ]
}

But this fails with the following error:

Error: process exited with status -1 (cannot attach to process due to System Integrity Protection)

Now I know I can just go and disable SIP but I'd like to avoid that. Any help will be appreciated...

vknabel commented 6 years ago

Debugging UITests would be interesting (and I would be happy to link any solution for this), but I am sorry to say, that I do not know enough about debuggers and especially about debugging iOS apps.

Maybe the idea proposed in https://github.com/vadimcn/vscode-lldb/issues/95 may help? Alternatively one could find out how ios-deploy works internally and write a small bash script as a solution, if possible.

If you write some comments, could you link me? I'd like to keep up to date on this topic.