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)
Community
  • 1
  • 1
Rhapsody
  • 6,017
  • 2
  • 31
  • 49

10 Answers10

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.

Snowbear
  • 16,924
  • 3
  • 43
  • 67
  • 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 again – Rhapsody Nov 03 '11 at 11:22
  • 2
    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. – JoeBilly Nov 03 '11 at 11:50
  • Unfortunately, the diagnostic log doesn't tell me something new. The exact same error message without any further (useful) details. – Rhapsody Nov 03 '11 at 12:21
  • 1
    @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. – Snowbear Nov 03 '11 at 12:26
  • @Snowbear 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!) – Rhapsody Nov 03 '11 at 12:28
  • 10 years later, this is still useful – Sergio Mazzoleni Sep 15 '21 at 07:58
7

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!

Community
  • 1
  • 1
Rhapsody
  • 6,017
  • 2
  • 31
  • 49
  • 4
    I noticed this as a solution to the error. But now all of my tests that relied on that accessor no longer compile. – Joel McBeth Jul 06 '12 at 12:56
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.

jasonoriordan
  • 873
  • 16
  • 25
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.

JoeBilly
  • 3,057
  • 29
  • 35
  • Thanks for the comment, but I'm sure I'm using the debug folder. I already got the exception when compiling the solution. – Rhapsody Nov 03 '11 at 11:22
  • 1
    This could be my problem, but how does one edit the "test running/launching folder"? – FizxMike Jan 16 '18 at 16:07
  • 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 : https://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. – JoeBilly Aug 29 '18 at 15:34
1

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

lolelo
  • 698
  • 6
  • 18
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.

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).

Eric Labashosky
  • 29,484
  • 14
  • 39
  • 32
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.

Dan Rayson
  • 1,315
  • 1
  • 14
  • 37
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

Vagaus
  • 4,174
  • 20
  • 31
  • The 'Assembly Binding Logviewer' shows up with an empty screen and the 'view log' button disabled. – Rhapsody Nov 03 '11 at 11:29
  • Have you enabled assembly loading log? Its disabled by default – Vagaus Nov 03 '11 at 12:33
  • 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. – Rhapsody Nov 03 '11 at 12:59
-1

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

zzfima
  • 1,528
  • 1
  • 14
  • 21