Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A simple and sweet VSCode extension to unit test C++ code.

License

NotificationsYou must be signed in to change notification settings

mattrussell2/vscode-cpp-unit-test

Repository files navigation

A simple and sweet VSCode extension to unit test C++ code.

vscode_unit_test_video.mov

Setup

To use the extension, you'll need to:

  1. Create a unit_tests.h file
  2. Set up your Makefile
  3. Start testing your code!

NOTE: Please follow these instructions carefully!

Getting started

If you haven't already,install the C++ Unit Testing Framework by AutumnMoon from the VSCode Marketplace.

Then, create and move into a new folder.

mkdir unittest_demo cd unittest_demo

Now, create a Makefile and unit_tests.h file:

touch Makefile unit_tests.h

Wonderful! Now, open this folder in a new workspace:

code .

If you see the testing panel open on the left-hand window, like in the image below then you're good to go. If not, ensure that the above two files are in your cwd, and again runcode . again (the extension will only activate if you have a file namedunit_tests.h in your workspace on launch of the VSCode instance, which minimizes its footprint in other contexts). Once the extension is loaded in your VSCode workspace, it will discover tests as you write them, and will recompile your code automatically when you run your tests.

image

Note! Whenever you plan to use the unit_test framework, please runcode . to initialize a workspace in the directory where you are working. This way, the correctunit_tests.h script will be used!

unit_tests.h

Now, we'll editunit_tests.h. This file will hold your test functions, each of which:

  1. returnsvoid
  2. takes no arguments
  3. has a unique test function name

For instance, yourunit_tests.h file might read:

#include<cassert>voidtest_pass() {assert(0 ==0);}voidtest_fail() {assert(1 ==0);}voidtest_fail_valgrind() {int *x =newint[100];}

assert statements are encouraged.

Each test will effectively be run as its own 'main' by a driver that the extension creates for you. Valgrind will also be run on the test.

A test is considered successful if it finishes execution; it will fail if either the main test fails or if valgrind fails (valgrind is run with --leak-check=full and --show-leak-kinds=all).

Also, tests that diff against expected output are supported. Any file with the same name as a test in a folder named stdout/ will automatically be diff'd against the stdout of that test. See the sample/ for examples.

The last thing to do is to set up the Makefile.

Makefile

YourMakefile will need a rule namedunit_test which links your code with a file namedunit_test_driver.o. This is a custom driver file built by the extension that includes amain function.

CXX: clang++unit_test: unit_test_driver.o$(CXX) unit_test_driver.o

Save

Make sure you've saved both files.

Run Your Tests

Navigate back tounit_tests.h. You should see 'play' buttons next to each test declaration.

image

Press each one to play its test.

image

You can also head to the 'test explorer panel, where there is an overview of all tests, and a button to run all tests together.

image

You can also create test groups with a comment above the first test in a group as follows:

/* TEST GROUP SOMENAME */

(A regex is looking for the phraseTEST GROUP).

image

Each group will run all tests below it (other groups excluded). If you want to create even more distinct test groups, you can create any number of files named XXX_tests.h, each of which has testing functions. These will be able to be run independently of one another. Note that each such file in your workspace will be#included in the testing driver automatically; also, each test function name even across multiple files must be unique. When running test groups or all tests from all files, the code will be compiled only once.

That's it! Happy testing :)

Matt

PS: Many thanks tohttps://github.com/microsoft/vscode-extension-samples for having some great examples to work from.

Changelog

0.4.5

  • test group naming fixed, and unhierarchicalized the groups.

0.4.2

  • updated compilation process to runmake unit_test_driver.o so theMakefile will be used rather than forcingclang++

About

A simple and sweet VSCode extension to unit test C++ code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp