Page 1 of 1

Building and running unit_test example on vscode extension

Posted: Tue Apr 12, 2022 1:51 pm
by martin1678
I am trying to build the esp-idf unit_test example. I can build the project, but not the one including the test, has any done this? can give me some pointer on how?

Re: Building and running unit_test example on vscode extension

Posted: Wed May 18, 2022 7:40 am
by ashelman-ec
I have the same question...

Re: Building and running unit_test example on vscode extension

Posted: Wed May 18, 2022 1:35 pm
by ashelman-ec
OK, so I am new to ESP32 and VS Code, but after studying about VS Code tasks, here is how I was able to build, flash, and run the unit tests for Espressif's examples / system / unit_test project.

Basically, the .vscode/tasks.json file defines tasks (such as build, flash, clean, and monitor) and these are all run from the main project directory. We need equivalent commands to be run from within the test/ directory. Changing the working directory which the task is executed from is accomplished using cwd inside of the options.

So, find the relevant section in the .vscode/tasks.json file, insert a copy of it at the end of the file, modify the label so it is unique, and insert cwd into the options. Also, these can all be grouped as test tasks. For example, here is my Test Build label:

Code: Select all

        {
            "label": "Test Build - Build unit tests",
            "type": "shell",
            "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py build",
            "windows": {
                "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py build",
                "options": {
                    "env": {
                        "PATH": "${env:PATH};${config:idf.customExtraPaths}"
                    },
                    "cwd": "${workspaceFolder}\\test"
                }
            },
            "options": {
                "env": {
                    "PATH": "${env:PATH}:${config:idf.customExtraPaths}"
                },
                "cwd": "${workspaceFolder}/test"
            },
            "problemMatcher": [
                {
                    "owner": "cpp",
                    "fileLocation": [
                        "relative",
                        "${workspaceFolder}"
                    ],
                    "pattern": {
                        "regexp": "^\\.\\.(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                },
                {
                    "owner": "cpp",
                    "fileLocation": "absolute",
                    "pattern": {
                        "regexp": "^[^\\.](.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            ],
            "group": {
                "kind": "test",
                "isDefault": false
            }
        }
Here are the five lines from above that were modified/inserted (note that one also has to add commas after some braces; the IDE will highlight JSON syntax problem areas to prompt you for them):

Code: Select all

            "label": "Test Build - Build unit tests",
                    "cwd": "${workspaceFolder}\\test"
                "cwd": "${workspaceFolder}/test"
                "kind": "test",
                "isDefault": false
Similarly, create new tasks Test Flash and Test Clean. The existing ESP-IDF Monitor device icon at the bottom of the screen can be used without modification after Test Flash. The Task Monitor: Start the monitor command will flash the main code instead of the unit tests, so one could create a new Test Monitor task if desired, and make sure to modify its dependsOn to point to Test Flash instead.

There are two ways to run these tasks:
  • type (Ctrl+P), type "task " (make sure to type a space after task) and select from the list
  • open the Command Palette (Ctrl+Shift+P), type "Tasks run test task", hit "Return" and select the task from the list
This worked for me! If anyone has a more elegant solution, please post!

Re: Building and running unit_test example on vscode extension

Posted: Fri May 20, 2022 6:59 am
by ashelman-ec
Next question: How to debug when running the unit tests?

I read here and tried the custom settings described here, but no luck.

When a unit test fails, it sure would be useful to run the debugger on it!

Anybody know how to do this?