dotMemory Unit is an additional framework that pairs with popular unit testing frameworks. Its purpose is to let you write tests that check your code for all kinds of memory issues. For example, these can be tests that determine leaks by checking memory for objects of a particular type, or tests that track memory traffic and fail in case the traffic exceeds some threshold. In other words, dotMemory Unit extends the possibilities of your unit testing framework with the functionality of a memory profiler. More specifically, dotMemory Unit allows:
checking memory for objects of a certain type,
checking memory traffic,
getting difference between memory snapshots,
saving memory snapshots for further investigation in dotMemory (standalone .NET memory profiler).
How dotMemory Unit works
To run tests with dotMemory Unit support, you can use the ReSharper unit test runner. In this case, you should have Visual Studio with ReSharper, JetBrains dotCover, or JetBrains Rider installed on your machine. Another option is to run tests using the standalone dotMemory Unit launcher (available in both NuGet package and the zip file). Learn how to use the launcher in Use dotMemory Unit Standalone Launcher.
After you install the dotMemory Unit package, ReSharper's/Rider's menus for unit tests will get the additional Run Unit Tests under dotMemory Unit item. In this mode, test runner will execute dotMemory Unit framework calls as well as ordinary test logic. If you run a test in an "ordinary way" (without dotMemory Unit support), depending on the settings, either the test will fail or dotMemory Unit calls will be ignored.
dotMemory Unit works with almost all of the popular unit-testing frameworks including MSTest, NUnit, and xUnit.net.
How does it look
To quickly introduce you to the framework, let's consider the following test example.
This NUnit test selects all objects of the
Foo type from memory and asserts that there are no such objects. Such a test is quite common when you look for memory leaks in your code.
Note that dotMemory Unit does not force you to use any specific
Assert syntax. Use the syntax of the framework your test is written for. For example, the shown assertion uses the NUnit syntax but could be easily updated for MSTest *:
When to use dotMemory Unit
Use "memory" tests in the same way as ordinary unit tests:
After you manually find an issue (such as a leak), write a memory test that covers it.
Write tests for proactive testing to ensure that new product features do not create any memory issues, e.g., objects left in memory or significant memory traffic.
dotMemory 10 – 2017.1
dotMemory 2017.3 – 2019.1
dotMemory Unit 1.0 – 2.3
dotMemory Unit 2.4
dotMemory Unit 3.0 – 3.2