Visual Studio Code Run Unit Tests C#

Posted on  by 



Disclaimer

Running the test and checking the test results. In Microsoft Visual Studio, a special menu of commands, called Test, is implemented to work with Unit-tests. To run the test, you must select one of the commands. Test - Run - Tests in Current Context. Test - Run - All Tests in Solution. As shown in Figure 13. In the first one, we only want to run the Unit Tests because they are quick in execution and work as a first line of defense. Integration Tests take longer, and you want to run them once a night i.e. In a 'CI Nightly Build'. Run only Unit Tests. In the VSTS Build configuration, you can still use the standard 'Visual Studio Test' task. Type unit test in the search box, select C# as the language, and then select the C# Unit Test Project for.NET Core template, and then click Next. CTRL + R, then press C This runs all the tests in the current test class. CTRL + R, then press N This runs all tests in the current namespace. To run tests from source code files in your solution, by using the keyboard 1. In Visual Studio, open a source code file anywhere in your solution. You can use the following keyboard shortcuts to run.

This extension is for the C Unit Test Framework by ThrowTheSwitch.org, not for the Video Engine.

Run your Unity tests using theTest Explorer extension.

Features

  • Shows a Test Explorer in the Test view in VS Code's sidebar with all detected tests and suites and their state
  • Adds CodeLenses to your test files for starting and debugging tests
  • Adds Gutter decorations to your test files showing the tests' state
  • Adds line decorations to the source line where a test failed
  • Shows a failed test's log when the test is selected in the explorer
  • Lets you choose test suites that should be run automatically after each file change

Getting started

Visual Studio Code Run Unit Tests C#
  • Install the extension and restart VS Code
  • Open the workspace or folder containing your Unity framework project
  • Configure the extension based on your project's needs
  • Open the Test view
  • Run your tests using the icons in the Test Explorer or the CodeLenses in your test file
Visual Studio Code Run Unit Tests C#

Configuration

Options

Example folder structure:. - the root of the workspace./makefile - the makefile which describes how to build the unit testsfolders - a target in the makefile which creates needed folder structure (i.e. creates ./out)./src - the folder where all tested sources are to be found./src/foo.c - a testable source filebar() - a function in foo.c which should be unit tested./testsrc - the folder where the unit test sources are to be found./testsrc/fooTest.c- a unit test for foo.ctest_bar1() and test_bar2() - actual unit tests: functions in fooTest.c which test bar() in foo.c./out/test - the path to the built unit tests./out/test/fooTest.exe - the executable which would be built by the extension to run the tests in fooTest.c

PropertyDescriptionExample
unityExplorer.debugConfigurationConfiguration to run when debugging tests. See Debugging for more info.Unit Test
unityExplorer.prettyTestCaseRegexRegular expression to be used to simplify the test case label which by default is the full function name. Put what you want to keep in a single matching group and the rest will be removed. Leave blank to disable this.
Inactive:
Active:
test_(w+)
unityExplorer.prettyTestFileRegexRegular expression to be used to simplify the test file label which by default is the path relative to the source folder. Put what you want to keep in a single matching group and the rest will be removed. Leave blank to disable this.
Inactive:
Active:
(w+)Test.c
unityExplorer.unitUnderTestFolderThe path to the unit under test source files. By default the workspace root is used.src
unityExplorer.unitUnderTestFileRegexRegular expression to find unit under test source files. These are tracked for changes to mark test results as old if not re-run.w+.[ch]
unityExplorer.testSourceFolderThe path to the unit test source files. By default the workspace root is used.testsrc
unityExplorer.testSourceFileRegexRegular expression to find test source files.w+Test.c
unityExplorer.testCaseRegexRegular expression to find test cases in a file. The actual test case name must be put in a single matching group.voids+(test_.*)s*(.*)
unityExplorer.preBuildCommandAny command which must be run before building the unit tests. If empty, no command will be run.make clean
unityExplorer.testBuildApplicationApplication used to build the tests (e.g. make, cmake, gcc). A test will be built by running this and the test's file name, with the system's executable extension (i.e. make unitTest.exe).make
unityExplorer.testBuildCwdPathThe current working directory where the build command will be run in. By default the workspace root is used..
unityExplorer.testBuildArgsAny additional arguments that need to be passed to the build command when building a test. Note that the test executable target is already passed, so there is no need to add it here.-DTEST
unityExplorer.testExecutableFolderThe path to the built output files. By default the workspace root is used.out/test

Commands

The following commands are available in VS Code's command palette, use the ID to add them to your keyboard shortcuts:

IDCommand
test-explorer.reloadReload tests
test-explorer.run-allRun all tests
test-explorer.run-fileRun tests in current file
test-explorer.run-test-at-cursorRun the test at the current cursor position
test-explorer.cancelCancel running tests

Debugging

To set up debugging, create a new Debug Configuration.${command:unityExplorer.debugTestExecutable} can be used access the test executable filename being ran, with the relative path to it configured by unityExplorer.testBuildPath.Then, edit the unityExplorer.debugConfiguration settings with the name of the Debug Configuration to run during debug.

Note: Individual test debugging is not supported. Instead the entire test file will be ran, so skip or remove breakpoints accordingly.

Example configuration for gdb:

Tests

Troubleshooting

Visual Studio Code Unit Test

If you think you've found a bug, please file a bug report.

Visual Studio Unit Test Project

Tests

Visual Studio Create Unit Test

Project inspired by vscode-ceedling-test-adapter





Coments are closed