matepek / vscode-catch2-test-adapter

Catch2, Google Test and doctest Adapter for the VSCode
https://marketplace.visualstudio.com/items?itemName=matepek.vscode-catch2-test-adapter
MIT License
210 stars 52 forks source link

Is it possible to call cmake build before run? #127

Closed xgdgsc closed 5 years ago

xgdgsc commented 5 years ago

Is your feature request related to a problem? Please describe. Currently when I change code and want to test and click run. It runs the old binary.

Describe the solution you'd like Call cmake build first before run.

matepek commented 5 years ago

It works the other way around. You build your binary and the extension detects it and re-run it. Check this conversation for further details. enable autorun

For building from vscode: check this.

Here is my tasks.json for example:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "clean rebuild (debug)",
            "type": "shell",
            "group": "none",
            "command": " rm -rf debug; mkdir debug; cd debug; cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug ../.. && ninja",
            "options": {
                "cwd": "${workspaceFolder}/out"
            },
            "windows": {
                "command": "cmd.exe",
                "args": [
                    "/C",
                    "cd . && cd debug && del /S /Q /F * >NUL && vcvarsall.bat amd64 && cmake -DCMAKE_BUILD_TYPE=Debug -G Ninja ../.. && cd ."
                ]
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            },
            "problemMatcher": []
        },
        {
            "label": "ninja (debug)",
            "type": "process",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command": "ninja",
            "options": {
                "cwd": "${workspaceFolder}/out/debug"
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            },
            "problemMatcher": {
                "base": "$gcc",
                "fileLocation": [
                    "relative",
                    "${workspaceFolder}/out/debug"
                ]
            },
        }
    ]
}
xgdgsc commented 5 years ago

OK. Thanks.

matepek commented 4 years ago

In case somebody find their way here: https://github.com/matepek/vscode-catch2-test-adapter/blob/master/documents/support.md

yudi-azvd commented 3 years ago

@xgdgsc I found a way to run tests automatically. Install the extension Trigger Task on Save and configure it to trigger a task with the label, let's say, "Build tests" when files are saved. You can choose which files with patterns (src/*.cpp, tests/*.cpp).

Then create a task in .vscode/tasks.json labeled "Build tests" which will build your tests by calling make, g++ or something else you use in your project. I'm using something like this and it's working fine for now:

{
      "label": "Build tests",
      "type": "shell",
      "presentation": {
        "echo": false,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
        "showReuseMessage": false,
        "clear": true
      },
      "command": "make tests", //shell/terminal command to build tests
      "problemMatcher": []
    }

And make sure to have autorun from the Test Explorer enabled like matepek mentioned.

Hope it is useful.

George-Gi commented 1 year ago

I am looking for a way to first build the tests, if a source file has changed, and then run the tests. Ideally this sequence taking place by hitting the "Run Tests" icon. In the TestMate version 4.3.5 I don't see the option "Enable Autorun". Any suggestions?

appden commented 1 year ago

Just in case this helps, I have it setup to build my tests with cmake before running tests in the UI.

My settings.json includes this:

    "testMate.cpp.test.advancedExecutables": [
        {
            ...
            "runTask": {
                "before": [
                    "build tests",
                ],
            },
        },
    ],

And tasks.json leverages CMake Tools extension to run the build:

        {
            "label": "build tests",
            "type": "cmake",
            "command": "build",
            "group": "build",
            "targets": [
                "test/all"
            ]
        },
jnz86 commented 11 months ago

Just in case this helps, I have it setup to build my tests with cmake before running tests in the UI.

So, I like this and tried it, but ran into an issue.

If you mess up your build, you might miss something important. For me what would happen is my test would attempt to build, fail, the old exec would still be there and the test would run it. Likewise, you can't clean the old execs, because the test explorer won't see them anymore.

For me, it would attempt to build automatically when I clicked test, fail build, then report a success or fail (whatever my last correctly built test was). If I didn't look to see in my output that it failed, it could be easy to miss.

Mate responded to my question about this that it wasn't intended to run like that and it was probably better to run the tests when the exec changes. This way, a failed build won't run a test at all.

For me, this changes my flow to build tests in order to run them, instead of clicking test to build then run. It's a small difference but important.