Building and running unit_test example on vscode extension

martin1678
Posts: 1
Joined: Tue Apr 12, 2022 1:42 pm

Building and running unit_test example on vscode extension

Postby martin1678 » Tue Apr 12, 2022 1:51 pm

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?

ashelman-ec
Posts: 9
Joined: Fri May 13, 2022 6:08 am

Re: Building and running unit_test example on vscode extension

Postby ashelman-ec » Wed May 18, 2022 7:40 am

I have the same question...

ashelman-ec
Posts: 9
Joined: Fri May 13, 2022 6:08 am

Re: Building and running unit_test example on vscode extension

Postby ashelman-ec » Wed May 18, 2022 1:35 pm

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!

ashelman-ec
Posts: 9
Joined: Fri May 13, 2022 6:08 am

Re: Building and running unit_test example on vscode extension

Postby ashelman-ec » Fri May 20, 2022 6:59 am

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?

pmontes
Posts: 3
Joined: Fri Dec 13, 2024 9:10 am

Re: Building and running unit_test example on vscode extension

Postby pmontes » Tue Mar 11, 2025 10:29 am

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!
Hello, your solution helped me! Thank you.

In my case, using Powershell I loaded the environment, appended flash and monitor to the task and assigned a keybinding

Code: Select all

{
        "label": "Test Build - Build and flash unit tests",
        "type": "shell",
        "windows": {
            "command": "powershell.exe",
            "args": [
              "-ExecutionPolicy", "RemoteSigned",
                "-Command",
                "${config:idf.espIdfPathWin}\\export.ps1; idf.py build flash monitor -p ${config:idf.portWin}"
            ],
            "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
        }
    }
Note "options" and "problemMatcher" remain the same. To assign the keybinding in keybindings.json, for example Ctrl + E, U (unit tests)

Code: Select all

[
    {
        "key": "ctrl+e u",
        "command": "workbench.action.tasks.runTask",
        "args": "Test Build - Build and flash unit tests"
    },
]

bignacio
Espressif staff
Espressif staff
Posts: 255
Joined: Wed May 02, 2018 12:12 pm

Re: Building and running unit_test example on vscode extension

Postby bignacio » Fri Mar 14, 2025 6:49 am

You can also try our ESP-IDF vscode extension unit testing features.

Documentation is here: https://docs.espressif.com/projects/vsc ... sting.html

Let me know how it goes !

Who is online

Users browsing this forum: No registered users and 1 guest