Unit test project cannot find assembly under test (or dependencies)
Asked Answered
P

11

14

When I try to run my Unit Test project, I get the following error:

Could not load file or assembly 'ASSEMBLY_NAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

The system cannot find the file specified.

The assembly under test is located in the same solution and every project target .NET 4.0 framework. It worked for a while, but today I got this error again. I can't figure out what is wrong, because the error message doesn't tell me enough. (like; WHICH file could not be found..)

I've tried to add a new Test project, but also that one worked for a while before it gave me this same error message.

Is there anybody who can point me in the right direction?

Edit: In the diagnostic build log, the following error appears:

C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\TeamTest\
Microsoft.TeamTest.targets(14,5): 
error : Could not load file or assembly 'ASSEMBLY_NAME, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null' or one of its dependencies. 
The system cannot find the file specified.

Done executing task "BuildShadowTask" -- FAILED. (TaskId:671)

Done building target "ResolveTestReferences" in 
      project "ASSEMBLY_NAME.Tests.vbproj" -- FAILED.: (TargetId:985)
Phonography answered 3/11, 2011 at 10:38 Comment(0)
P
8

After checking out some additional search results I came across this question here at SA Private Accessor for method is not found .

After I deleted the *.accessor file, my test project stopped complaining and compiled succesfully.

Everybody thanks for their help!

Phonography answered 4/11, 2011 at 7:40 Comment(1)
I noticed this as a solution to the error. But now all of my tests that relied on that accessor no longer compile.Limnology
L
10

Check Build->Configuration Manager... menu. All projects should have the same platform and column Build checked.

Also you can take a look into output of the build (Output window), it usually states more clearly which file could not be found.

Levan answered 3/11, 2011 at 10:47 Comment(6)
Thanks for the comment. Every project inside the solution is set to debug/anyCpu. At first sight, the Output window did not give any more details. I will check it againPhonography
In Tools > Options > Projects & Solutions > Build and Run, you can set MSBuild project build ouput verbosity to Detailed or Diagnostic to check what ResolveAssemblyReferences targets are saying.Deter
Unfortunately, the diagnostic log doesn't tell me something new. The exact same error message without any further (useful) details.Phonography
@Rhapsody, I've tried to reproduce the issue, in case of assembly or dependency was not found I have the following in output window: error CS0006: Metadata file '{FULL_ASSEMBLY_PATH}' could not be found. Please be sure that show output from : Build is selected.Levan
@Levan unfortunately, the keywords 'CS0006', 'Metadata' and 'Could not be found' do not appear in the ouput log (with Build selected). Thanks a lot for trying to reproduce the issue though!)Phonography
10 years later, this is still usefulClerihew
P
8

After checking out some additional search results I came across this question here at SA Private Accessor for method is not found .

After I deleted the *.accessor file, my test project stopped complaining and compiled succesfully.

Everybody thanks for their help!

Phonography answered 4/11, 2011 at 7:40 Comment(1)
I noticed this as a solution to the error. But now all of my tests that relied on that accessor no longer compile.Limnology
F
5

I tried all above to no avail. Finally, in the csproj i changed

<Private>False</Private>

to

<Private>True</Private>

on the references and all tests passed.

Farias answered 9/2, 2016 at 15:15 Comment(2)
Same here, but why?Lama
Found the following for explanation: https://mcmap.net/q/142744/-what-does-the-private-setting-do-on-a-projectreference-in-a-msbuild-project-fileLama
D
3

There is a common error about executing unit tests : the execution folder.

Are you sure your unit test is running in the correct folder ? Not in the famous obj folder ?

In the obj folder, only generated assemblies are copied, not dependencies -even copy local true-. So if your test is launched from this folder, all the dependencies will missing.

Deter answered 3/11, 2011 at 11:12 Comment(3)
Thanks for the comment, but I'm sure I'm using the debug folder. I already got the exception when compiling the solution.Phonography
This could be my problem, but how does one edit the "test running/launching folder"?Colchicum
In most cases, the test runner (MSTest, NUnit...) processes unit tests within a folder. Check the test runner command line in logs. A common mistake is to have an undefined test folder which imply to run ALL tests found in the solution and its sub-directories. Ex for MSTest : msdn.microsoft.com/en-us/library/ms182489.aspx. Defining the test folder is another question, it depends on the product running the tests and the execution context.Deter
M
1

Make sure Dependencies > Edit References > Project (you project) is selected and not .net Assembly of the project

Maguire answered 3/11, 2018 at 16:30 Comment(0)
G
0

Maybe enabling assembly loading logging can give you a hint about why your assembly cannot be loaded.

http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

Greeneyed answered 3/11, 2011 at 11:14 Comment(3)
The 'Assembly Binding Logviewer' shows up with an empty screen and the 'view log' button disabled.Phonography
Have you enabled assembly loading log? Its disabled by defaultGreeneyed
After a second start (and logging turned on) I was able to see some logging. I haven't found anything useful yet, but I keep looking.Phonography
F
0

I had the same issue and found that copying the dll's to the GAC resolved the issue. Though I still don't understand why it doesn't use the referenced projects assemblies.

Fulgurating answered 15/8, 2012 at 12:52 Comment(0)
C
0

Another possible cause is if the assembly name of unit test project is using the same name as the assembly under test. (ie make sure your unit test assembly name is unique).

Charlean answered 29/1, 2016 at 15:13 Comment(0)
F
0

I had this error and I fixed it by UPDATING NUGET PACKAGES.

This worked because one of my projects in the solution was referencing a very old compiled version of another project that was contained in the same solution.

When I used "Run All" tests in Visual Studio, I suppose it copies over the "current" outputs of the projects, and then the nuget package dependency dlls afterwards, which overwrites the originals if they have the same name. (total guesswork there)

When I inspected the error message, I could see that my project was referencing "version 0.0.10" of a dependency, but my solution contained the project all the way up to "version 0.0.30", so massive difference.

Fiche answered 31/1, 2019 at 16:46 Comment(0)
D
0

In my case the unit test project file needed to match the main project file for both target framework and target platform:

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PlatformTarget>x86</PlatformTarget>
Daliadalila answered 11/7 at 19:43 Comment(0)
G
-1

References->Mark dlls->right click->properties->Copy Local set to true

Germicide answered 19/2, 2013 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.