I have a simple executable project and a Unit Test project all in the same solution in Visual Studio 2013. All my unit tests work fine in Visual Studio. I wanted to play* with the console app vstest.console.exe but am having a hard time getting going. I suspect I'm missing something very simple. I tried going to the directory where the actual .exe is and ran:
C:\BuildTest\Frame\Frame\obj\Debug>vstest.console.exe
C:\BuildTest\Frame\FrameUnitTests\obj\Debug\FrameUnitTests.dll
/Platform:x64 /Tests:ConstructorTest
and I got the error:
Starting test discovery, please wait...
Failed ConstructorTest
Error Message:
Test method FrameUnitTests.MainWindowPresenterTests.ConstructorTest threw
exception:
System.IO.FileNotFoundException: Could not load file or assembly
'Frame, Vers ion=3.0.0.0, Culture=neutral, PublicKeyToken=null' or one
of its dependencies. T he system cannot find the file specified.WRN:
Assembly binding logging is turned OFF.
I also tried running it from the actual directory where the unit test dll is:
C:\BuildTest\Frame\FrameUnitTests\obj\Debug>
and got the same results.
The error indicates that I need to somehow tell vstest.console where to find the main assembly that is being tested. How do I do this? Can someone point me to an example?
I'm interested in playing with vstest.console.exe for the following reason: I would like to get a printout of all my unit tests (about 80 of them) for use in documentation. I can't find a good way to do this. It occurred to me that vstest.console might be helpful for this. If there is another way to get a list of all my tests, I'd like to know. Admittedly, I could have simply typed every test title into MSWord in the time I've worked on this, but I'm a glutton for punishment and it might be nice to know how to use vstest.
I tried John Koerner's suggestion (see his answer) to get a list of tests and got the following results which might be a hint as to what I have going wrong:
C:\BuildTest\Frame\FrameUnitTests\obj\Debug>vstest.console.exe /ListTests:
FrameUnitTests.dll
Microsoft (R) Test Execution Command Line Tool Version 12.0.21005.1
Copyright (c) Microsoft Corporation. All rights reserved.
Test run will use DLL(s) built for framework Framework45 and platform X86. Follo
wing DLL(s) will not be part of run:
ACMFrameUnitTests.dll is built for Framework Framework45 and Platform X64.
Go to http://go.microsoft.com/fwlink/?LinkID=236877&clcid=0x409 for more detail
s on managing these settings.
Error: None of the provided test containers match the Platform Architecture and
.Net Framework settings for the test run. Platform: X86 .Net Framework: Frame
work45. Go to http://go.microsoft.com/?link for more details on managing these
settings.
If you only need a list of tests, you can simply run the following command:
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\ide\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /ListTests:UnitTests.dll
Related
I'm trying to set up a C# project with the latest version of Visual Studio, 2022; this is with .Net 6 on Windows 10. It's a simple console program, and I've set up the project and a corresponding unit test project basically following the steps described in https://learn.microsoft.com/en-us/visualstudio/test/walkthrough-creating-and-running-unit-tests-for-managed-code?view=vs-2022 so I've got something isomorphic to that tutorial project.
And the unit tests work fine when run from within Visual Studio.
Now I want to also run them from the command line.
vstest.console bin\Debug\net6.0\foo.dll
gives
Testhost process exited with error: Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at Microsoft.VisualStudio.TestPlatform.TestHost.Program.Main(String[] args)
. Please check the diagnostic logs for more information.
Looks like the toolchain is failing to find one of its own libraries? Is there some option I need to be specifying?
Make sure that foo.dll is the project which contain the tests and reference MSTest.TestFramework if you follow convention naming, this should be fooTests.dll
vstest.console bin\Debug\net6.0\fooTests.dll
in the example you mention the following line will throw error
vstest.console ..\BankTests\bin\Debug\net6.0\Bank.dll
While this will run normally
vstest.console ..\BankTests\bin\Debug\net6.0\BankTests.dll
After upgrading from .Net 5 to .Net 6, I had to face the same issue.
Add the following two lines to your .csproj file to fix the issue.
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<GenerateRuntimeConfigDevFile>true</GenerateRuntimeConfigDevFile>
Example:
I have a solution, which contains numerous projects. Each of these projects has an associated test project which uses MSTest V2. Some of these test projects take advantage of Microsoft Fakes.
When I run the tests in the Visual Studio IDE, everything works fine. However, my build pipeline fails. I attempted to emulate what my build pipeline is doing and it also fails on my machine.
The error message I get is the following:
System.TypeLoadException: Could not load type
'System.Fakes.ShimDateTime' from assembly 'mscorlib.4.0.0.0.Fakes,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=0ae41878053f6703'.
After playing around with the vstest.console.exe application, it seems that if I run each test assembly separately all of the tests pass, but if I attempt to pass in multiple assemblies which utilize Microsoft Fakes they fail.
vstest.console.exe "Path/To/First.dll" Passes
vstest.console.exe "Path/To/Second.dll" Passes
vstest.console.exe "Path/To/First.dll" "Path/To/Second.dll" Fails
vstest.console.exe "Path/To/First.dll" "Path/To/Second.dll" /InIsolation Fails
I'm not sure what is going on here or how to resolve this issue.
I eventually figured out what the problem was:
Test Project A had not defined System.DateTime in a Microsoft Fakes config file, so an assembly was generated named mscorlib.4.0.0.0.Fakes.dll which was missing the System.Fakes.ShimDateTime.
Test Project B had defined System.DateTime in a Microsoft Fakes config file, so an assembly was generated named mscorlib.4.0.0.0.Fakes.dll which contained the type System.Fakes.ShimDateTime.
This lead the following structure:
ProjectA.Tests/bin/Debug/
ProjectA.Tests.dll -- Contains the unit tests for Project A
mscorlib.4.0.0.0.Fakes.dll -- Does not contain System.Fakes.ShimDateTime
ProjectB.Tests/bin/Debug/
ProjectB.Tests.dll -- Contains the unit tests for Project B
mscorlib.4.0.0.0.Fakes.dll -- Does contain System.Fakes.ShimDateTime
I'm not sure what the Visual Studio IDE is doing differently, but the vstest.console.exe seems to load all of the assemblies, ignoring duplicates based on fully qualified names. That is, it loads the following files:
ProjectA.Tests/bin/Debug/ProjectA.Tests.dll
ProjectA.Tests/bin/Debug/mcsorlib.4.0.0.0.Fakes.dll
ProjectB.Tests/bin/Debug/ProjectB.Tests.dll
It ignored the file ProjectB.Tests/bin/Debug/mcsorlib.4.0.0.0.Fakes.dll, which had the type System.Fakes.ShimDateTime defined within it.
The only solution to this is to run a separate vstest.console.exe per test project (instead of all at once), or to ensure that the generated Microsoft Fakes assemblies (*.Fakes.dll) have the same types defined across the board, so it doesn't matter which version of mscorlib.4.0.0.0.Fakes.dll is loaded. And, since the Azure Pipelines VsTest task uses a single run of vstest.console.exe this is important to know.
I'm using Visual Basic .Net, .NET-Verion 4.6.1, VS 2019 Community.
I want to include Unittests in my project so I istalled NUnit, created a Test-Project, imported the project I want to test, call a class in the project and I get:
SetUp failed for test fixture *Project*_Test.*Class*_Test
System.IO.FileNotFoundException : Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system can't find the required file.
So I searched around and installed the Config-Manager with Nuget-Console -> didn't do anything.
... copied the Config-Manager.dll from the project to my test-project -> different fileNotFound exception. This time it wants the next file.
So I debugged and saw, that the error originates on this line of code
Private ReadOnly STR_CONNECTION_STRING As String = DirectCast(New Global.System.Configuration.AppSettingsReader().GetValue("*DB_Conn_String*", GetType(String)), String)
How do I remove this error? The line of code doesn't seem to be the problem, since the test can't find the classes in the first place.
Thx for the help
Daedra
Okay, I spend a good day to figure this out:
If you use .NET-Framework for the project, you want to write tests for, you don't want to go
Add
-> New Project
-> NUnit Test (.Net-Core)
You run into unlimited problems with .NET-Core vs .NET-Framework.
Instead do
Add
-> New Project
-> Class-Library (.NET-Framework)
Then go to NuGet and install NUnit and NUnit3TestAdapter.
I simply copied my already written tests and they worked instantly.
Hope this helps someone someday ^~^
Cheers Daedra
We have a .NET solution that I'm trying to get to run in our new CI environment, TeamCity.
The solution builds and runs, and all unit tests run (not all pass, but that's a different story) on our dev machines.
It builds properly on the CI server as well, but when running the MSTest configuration, it fails, giving a message like:
Unable to load the test container '[one of my test assemblies]' or one of its dependencies. Error details: System.IO.FileNotFoundException: Could not load file or assembly '[one of my assemblies]' or one of its dependencies. The system cannot find the file specified.
Well, my assembly is built and available, so that's unlikely. On my local dev machine, I can get more detail with procmon or the like to see where the dependency chain is broken, but I'm not sure how I can do that against a remote server.
How can I find out what my CI server is missing to do its job? Is there some logging I can enable or something?
Sounds like the working directory is wrong when you are attempting to run tests.
Is your Build and Unit Tests steps in the same "Build Configuration"?
For about 2 weeks now, I have been unable to run any UnitTests (built in VS unit tests) for a project. Previously everything worked fine. The error message is:
Could not load file or assembly 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\MyProjectName.XmlSerializers.dll" or one of its dependencies.
The project references System.Xml.Serialization and uses the XmlSerializer class; just like many other classes/projects I've written. For some reason, only this project is affected. It builds fine, runs fine, I just can't run my unit tests.
I've checked the directory, and all the dlls in that directory are Microsoft dlls. The dll that it is looking for obviously is not a Microsoft dll.
Anyone have any ideas?
Edit:
It apparently has something to do with using the XmlSerializer and it generating that file automatically instead of using sgen.exe. Here is a link to the MSDN article. From what I've been able to find, it has something to do with using the serializer with generics. None of the sources I've found seem to offer any way to make it actually work.
First enable loader logging (using FUSLOGVW.exe from the SDK) to confirm what is not being found.
Then use Reflector on all your assemblies to find the one that is trying to load the non-existent assembly. If you find no such assembly it must be being loaded dynamically, in which case attaching to AppDomain.AssemblyResolve should allow you to identify where.
try to copy all your source files somewhere, then delete the project and try to make it from scratch. Maybe something happened with project dependencies
Is your computer 64bit? I got the same error when trying to run a 64bit dll with NUnit that was set to work as an x86 assembly (using corflags).
You can probably find out from the error message (use FUSLOGVW.exe lick Richard suggested).
If that is the case you can either sent the dll or NUnit to run as the correct assembly using corflags.
Solution
As it turns out, the problem was with VMWare. I installed the latest version of VMWare, and it installed it's tools to debug in a VM. Something it installed or changed caused this problem. When I uninstalled VMWare, the problem went away. So, I reinstalled VMWare without installing it's debugging capabilities and the problem did not come back.
Workaround:
I still have no idea why this problem suddenly started occurring, but I found a hack to make it work.
I had to go to project properties => Build Events and add this line to the Post-build event command line:
"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" "$(TargetPath)" /force
This forces VS to generate the file. I then had to copy that file manually to the directory it was looking for it in:
"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies"
Now it I can run my tests and step through them. The problems I have now are 1) I have to remember to copy the dll to that directory every time I change something in the classes that I am serializing, and 2) I now get a ThreadInterruptedException when a test finishes running; thus 3) I can only run one test at a time.
Not a good solution, but at least I can limp through. Unfortunately, redoing everything, as Nikita Borodulin suggested, is not an option.