This is my unittest:
Code: Select all
MemProfiler.AreAssertionsEnabled.Should().BeTrue();
MemProfiler.IsProfiling.Should().BeTrue();
using (MemProfiler.BeginTest(testName))
{
/* NOTE:
* When profiling a debug build of a program, the scope of the local variables is often longer than it might appear.
* This will prevent the instances from being collected, and the assertion will fail, falsely identifying the instances as a memory leak.
*/
void Iteration()
{
TestContext.WriteLine("Execute SuT ...");
doWork();
}
Iteration();
// get base snapshot
TestContext.WriteLine("Create base snapshot");
using (MemSnapshot fastSnapshot = MemProfiler.FastSnapshot())
using (MemSnapshot baseSnapshot = MemProfiler.FullSnapshot($"{testName} - Base snapshot")) // separate snapshot to compare in .NET Memory Profiler
{
fastSnapshot.Should().NotBeNull().And.NotBe(MemSnapshot.Empty);
baseSnapshot.Should().NotBeNull().And.NotBe(MemSnapshot.Empty);
Iteration();
MemProfiler.FullSnapshot($"{testName} - Comparison snapshot").Dispose(); // separate snapshot to compare in .NET Memory Profiler
using (AssertionsDefinition ad = MemAssertion.BeginAssertionsDefinition(fastSnapshot))
{
TestContext.WriteLine("Assert there are no new relevant instances ...");
// No new instances of any type in the Datev namespace should exist
ad.NoNewInstances("Nobody.*", true).NoNewInstances("Client.*", true)
// Since a timeobject is returned, we must allow 1 new instance of that type to be created.
// The MaxNewInstances assertion will override the NoNewInstances assertion above.
.MaxNewInstances(typeof(TaskResponse), 1);
// The AssertionsDefinition has been built, let’s perform the actual assertion.
MemAssertion.Assert(ad);
}
}
}
Code: Select all
VSTest.Console.exe Nobody.Test.dll --Settings:Nobody.Test\MemoryTest.runsettings
Code: Select all
using (MemSnapshot baseSnapshot = MemProfiler.FullSnapshot($"{testName} - Base snapshot"))
Code: Select all
Protokollname: Application
Quelle: Application Error
Datum: 28.06.2021 15:36:31
Ereignis-ID: 1000
Aufgabenkategorie:(100)
Ebene: Fehler
Schlüsselwörter:Klassisch
Benutzer: Nicht zutreffend
Computer: xxx
Beschreibung:
Name der fehlerhaften Anwendung: testhost.x86.exe, Version: 15.0.0.0, Zeitstempel: 0xe16c7da8
Name des fehlerhaften Moduls: clr.dll, Version: 4.8.4360.0, Zeitstempel: 0x606e7038
Ausnahmecode: 0xc0000005
Fehleroffset: 0x0033c7fc
ID des fehlerhaften Prozesses: 0x5660
Startzeit der fehlerhaften Anwendung: 0x01d76c22883da4c8
Pfad der fehlerhaften Anwendung: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe
Pfad des fehlerhaften Moduls: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Berichtskennung: 8f16caa2-2881-444b-aa56-ad424998de35
Vollständiger Name des fehlerhaften Pakets:
Anwendungs-ID, die relativ zum fehlerhaften Paket ist:
Ereignis-XML:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Application Error" />
<EventID Qualifiers="0">1000</EventID>
<Version>0</Version>
<Level>2</Level>
<Task>100</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2021-06-28T13:36:31.6334902Z" />
<EventRecordID>24062</EventRecordID>
<Correlation />
<Execution ProcessID="0" ThreadID="0" />
<Channel>Application</Channel>
<Computer>xxx</Computer>
<Security />
</System>
<EventData>
<Data>testhost.x86.exe</Data>
<Data>15.0.0.0</Data>
<Data>e16c7da8</Data>
<Data>clr.dll</Data>
<Data>4.8.4360.0</Data>
<Data>606e7038</Data>
<Data>c0000005</Data>
<Data>0033c7fc</Data>
<Data>5660</Data>
<Data>01d76c22883da4c8</Data>
<Data>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe</Data>
<Data>C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll</Data>
<Data>8f16caa2-2881-444b-aa56-ad424998de35</Data>
<Data>
</Data>
<Data>
</Data>
</EventData>
</Event>