Jeremy
Jeremy

Reputation: 636

Visual Studio Test Explorer doesn't run tests after changes

I am using Visual Studio 2022 Community Edition. Whenever I delete or rename existing test methods (including copy, paste, then rename), Visual Studio fails to run tests after the changes are made.

System.InvalidOperationException: TestsForOrigin: Unknown TestCaseRecord.Origin value Unknown
   at Microsoft.VisualStudio.TestStorage.MergedTestGroup.TestsForOrigin(TestCaseOrigin origin)
   at Microsoft.VisualStudio.TestStorage.MergedTestGroup.MarkAsStale()
   at Microsoft.VisualStudio.TestStorage.MergedTestIndex.MarkAsStale(TestCaseOriginKind originKind)
   at Microsoft.VisualStudio.TestStorage.TestStoreIndexSet.MarkTestsAsStale(TestCaseOriginKind originKind)
   at Microsoft.VisualStudio.TestStorage.TestStore.MarkTestsAsStale(TestCaseOriginKind originKind)
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.StartTestRun(IEnumerable`1 containers, IEnumerable`1 requestedTestCasesToRun, Boolean isHotReloadRun)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.StartTestRun(IEnumerable`1 containers, IEnumerable`1 requestedTestCasesToRun, Boolean isHotReloadedRun, Int32 totalIterations)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.StartTestRun(ICollection`1 requests, Int32 totalIterations)
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.Collections.Generic.KeyNotFoundException: The given key 'C:\Users\My Name Removed\source\repos\RepoNameRemoved\LibraryRenamed.ClassRenamed.Tests\bin\Debug\net6.0\LibraryRenamed.ClassRenamed.Tests.dll' was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToTestResultRecord(TestResultRecord testResult, IImmutableDictionary`2 containerLookup, TestStore store, Int32 testRunIndex, Boolean isHotReloadedRun, Int32 currentIteration)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.ToLocalTestResultRecord(TestResultRecord testResult)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResultRecords(IEnumerable`1 testResultRecords)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.AddRemoteTestResultRecords(IEnumerable`1 testResults)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunUpdate(IEnumerable`1 testResults, IEnumerable`1 activeTests)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.TestResponseHandler.<>c__DisplayClass16_0.<HandleTestRunStatsChangeAsync>b__0()
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.CallInternalWithLogging(IInternalLogger log, Action action, Boolean shouldThrow)
System.InvalidOperationException: TestsForOrigin: Unknown TestCaseRecord.Origin value Unknown
   at Microsoft.VisualStudio.TestStorage.MergedTestGroup.TestsForOrigin(TestCaseOrigin origin)
   at Microsoft.VisualStudio.TestStorage.MergedTestGroup.MarkAsNotRunningAndNotPending()
   at Microsoft.VisualStudio.TestStorage.MergedTestIndex.MarkAsNotRunningAndNotPending(TestCaseOriginKind originKind)
   at Microsoft.VisualStudio.TestStorage.TestStoreIndexSet.MarkTestsAsNotRunningAndNotPending(TestCaseOriginKind originKind)
   at Microsoft.VisualStudio.TestStorage.TestStore.MarkTestsAsNotRunningAndNotPending(TestCaseOriginKind originKind)
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.EndTestRun()
   at Microsoft.VisualStudio.TestWindow.Host.VsTestRunSession.OnTestRunCompleted()
   at Microsoft.VisualStudio.TestWindow.Utilities.EventPumpExtensions.<>c__DisplayClass0_0.<EnqueueAsync>b__0()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.<RunTestsAsync>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Host.TestPlatformProvider.<RunTestsAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.<CallWithCatchAsync>d__11`1.MoveNext()```

Upvotes: 21

Views: 7593

Answers (4)

Terje Solem
Terje Solem

Reputation: 926

I had this problem again now with renaming a test. Clean solution, and restart vs, and rebuild worked for me.

Upvotes: 0

haribo841
haribo841

Reputation: 31

Delete all .vs, .vscode, bin, obj folders from coresponding projects & solution.

Upvotes: 3

SunsetQuest
SunsetQuest

Reputation: 8817

Update 2/14/2023: issue resolved

Test Method rename fix is now out in Visual studio 17.4.5 (released 2/14/2023)
see change log with "Unit test startup fails with an exception when a test method is renamed"


Update 1/31/2023:

This issue will be resolved in VS 17.4.5 and 7.5 preview 4. (see https://github.com/microsoft/testfx/issues/1564)


I was having the same issue. Interestingly a simple restart of Visual Studio resolved it for me. It also looks like we can also downgrade the MSTest.TestAdapter and MSTest.TestFramework NuGet package to 2.2.8.

The restart idea came from here: https://github.com/microsoft/vstest/issues/4208

It seems to be a recent bug that started popping up last week. It looks like a fix has already in the works:

Osvaldo Calles [MSFT] : We already have a fix for this, but it is in code review. Unfortunately there is no workaround, other than downgrading the NuGet package version to 2.2.8. Source: https://developercommunity.visualstudio.com/t/Unit-test-startup-fails-with-an-exceptio/10226576

Upvotes: 29

Mutao
Mutao

Reputation: 1

I was having the same issue too. And I degrade the version of "Microsoft.NET.Test.Sdk" to "16.9.4", the version of "MSTest.TestAdapter" to "2.2.8", the version of "MSTest.TestFramework" to "2.2.8', just to run test_project normally

Upvotes: 0

Related Questions