19

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.

  • I can right click on previously run tests and run them, but any of the renamed tests I cannot. Likewise, any new tests that had been created since the last successful run until I renamed an existing test method.
  • I can create new tests from scratch with no issues as long as no other tests are renamed.
  • When I restart Visual Studio, all of the tests are recognized and run fine after the restart. But it's quite frustrating having to constantly restart Visual Studio while writing tests. I copy / paste a test method and rename it a lot, which causes this issue to happen every time.
  • I have tried removing and reinstalled Micosoft.NET.Test.Sdk, MSTest.Adapter and MSTest.TestFramework. I have deleted the repo, and re-cloned the repo essentially removing the hidden .vs folder.
  • This happens on two different computers, multiple solutions, both reference the same repos.
  • I have gone as far as creating a new .NET 6 Test project, and copy and pasted all the tests from the previous project to the new one. I do get several errors when I attempt to run the tests after doing so listed below.
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()```
Jeremy
  • 584
  • 5
  • 15
  • Did you check the Test Output? Go to Output and select "Test" in "Show output from:" checkbox. – Alberto Martín Dec 19 '22 at 12:31
  • 1
    Make sure all test classes and methods are public. Also, try deleting the `.vs` folder at the root of the visual studio solution. – Greg Burghardt Dec 19 '22 at 20:13
  • 1
    @AlbertoMartín the errors above are from the Test output. – Jeremy Dec 20 '22 at 12:57
  • @GregBurghardt I have set all of the classes and methods public. I've even go so far as to make my mock classes public vs internal. I have deleted the .vs folder several times. – Jeremy Dec 20 '22 at 12:58
  • 2
    This likely appears to be related to a bug with the following packages *MSTest.TestAdapter *MSTest.TestFramework Issues logged https://github.com/microsoft/vstest/issues/4208 https://developercommunity.visualstudio.com/t/Unit-test-startup-fails-with-an-exceptio/10226576 – Jeremy Dec 20 '22 at 15:47

3 Answers3

26

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

SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
  • 1
    This was in the comment I included above, however I'll mark it here as correct. It looks like a fix is incoming "soon". I can comment here again when it is live. – Jeremy Jan 06 '23 at 14:27
  • 1
    This issue has now been fixed. It will be available in the next preview release of VS 17.5. It should also be included in the next 17.4 update as well when that becomes available. – Jeremy Jan 12 '23 at 14:55
  • 1
    @Jeremy it is not yet fixed. I currently upgraded to the latest community version and the issue still persists. – Isaac Ikusika Jan 30 '23 at 11:13
  • 1
    Just updated to 17.4.4 of Visual Studio and the issue is present. Very frustrating. After every change to an existing test name, test execution fails. "Simply restarting" is not very useful when doing TDD and refactoring tests. – WesFanMan Jan 30 '23 at 19:36
  • I am having the same issue also with 17.4.4. It does seem like I need to restart VS less often, though.(maybe it is just me) It will be resolved in 7.4.5 and 7.5. (see github.com/microsoft/testfx/issues/1564) – SunsetQuest Jan 31 '23 at 16:39
  • I just had the same issue with v17.5.4 only restarting VS helped – mamen May 03 '23 at 10:07
2

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

haribo841
  • 21
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 22 '23 at 11:28
0

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

Mutao
  • 1