Skip to content

Commit 1e52428

Browse files
authored
Merge pull request #3052 from mikeblome/mb-codelens
CodeLens support in C++ unit tests
2 parents 3371ec2 + 66d2dce commit 1e52428

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed
30.3 KB
Loading
Loading

docs/test/writing-unit-tests-for-c-cpp.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
title: "Write unit tests for C/C++ in Visual Studio"
3-
ms.date: 11/04/2017
3+
ms.date: 10/09/2018
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-test
66
ms.topic: conceptual
77
ms.author: "mblome"
8-
manager: douge
9-
ms.workload:
8+
manager: wpickett
9+
ms.workload:
1010
- "cplusplus"
1111
author: mikeblome
1212
---
@@ -26,6 +26,10 @@ Visual Studio includes these C++ test frameworks with no additional downloads re
2626

2727
In addition to the installed frameworks, you can write your own test adapter for whatever framework you would like to use within Visual Studio. A test adapter can integrate unit tests with the **Test Explorer** window. Several third-party adapters are available on the [Visual Studio Marketplace](https://marketplace.visualstudio.com). For more information, see [Install third-party unit test frameworks](install-third-party-unit-test-frameworks.md).
2828

29+
**Visual Studio 2017 version 15.7 (Professional and Enterprise)**
30+
31+
C++ unit test projects support [CodeLens](../ide/find-code-changes-and-other-history-with-codelens.md).
32+
2933
**Visual Studio 2017 version 15.5**
3034

3135
- **Google Test Adapter** is included as a default component of the **Desktop development with C++** workload. It has a project template that you can add to a solution via the **Add New Project** context menu on the solution node in **Solution Explorer**, and options you can configure via **Tools** > **Options**. For more information, see [How to: use Google Test in Visual Studio](how-to-use-google-test-for-cpp.md).
@@ -74,13 +78,14 @@ TEST_CLASS and TEST_METHOD are part of the [Microsoft Native Test Framework](mic
7478
A TEST_METHOD returns void. To produce a test result, use the static methods in the `Assert` class to test actual results against what is expected. In the following example, assume `MyClass` has a constructor that takes a `std::string`. We can test that the constructor initializes the class as expected like so:
7579

7680
```cpp
77-
TEST_METHOD(TestClassInit)
78-
{
79-
std::string name = "Bill";
80-
MyClass mc(name);
81-
Assert::AreEqual(name, mc.GetName());
82-
}
81+
TEST_METHOD(TestClassInit)
82+
{
83+
std::string name = "Bill";
84+
MyClass mc(name);
85+
Assert::AreEqual(name, mc.GetName());
86+
}
8387
```
88+
8489
In the previous example, the result of the `Assert::AreEqual` call determines whether the test passes or fails. The Assert class contains many other methods for comparing expected vs. actual results.
8590
8691
You can add *traits* to test methods to specify test owners, priority and other information. You can then use these values to sort and group tests in **Test Explorer**. For more information, see [Run unit tests with Test Explorer](run-unit-tests-with-test-explorer.md).
@@ -106,6 +111,23 @@ For more information about using **Test Explorer**, see [Run unit tests with Tes
106111
107112
For best practices related to unit testing, see [Unit test basics](unit-test-basics.md)
108113
114+
## Use CodeLens
115+
116+
**Visual Studio 2017 version 15.7 Professional and Enterprise Editions only**:
117+
[CodeLens](../ide/find-code-changes-and-other-history-with-codelens.md) enables you to quickly see the status of a unit test without leaving the code editor. You can initialize CodeLens for a C++ unit test project in any of these ways:
118+
119+
- Edit and build your test project or solution.
120+
- Rebuild your project or solution.
121+
- Run test(s) from the **Test Explorer** window.
122+
123+
After **CodeLens** is initialized, you can see test status icons above each unit test.
124+
125+
![C++ CodeLens Icons](media/cpp-test-codelens-icons.png)
126+
127+
Click on the icon for more information, or to run or debug the unit test:
128+
129+
![C++ CodeLens Run and Debug](media/cpp-test-codelens-run-debug.png)
130+
109131
## See also
110132
111133
[Unit test your code](unit-test-your-code.md)

0 commit comments

Comments
 (0)