Build/Test Issues

NCrunch with external dependencies

Started by RaringCoder on 17,139 views

Hi,

I am trying to use NCrunch with SQL CE 4.0 and it cannot resolve the dependencies. Regular NUnit tests run fine, but NCrunch ones never do. I have noticed that the NCrunch working directory (whenever I happen to remember how to find it) appears to be missing most of the relevant dependencies.

I haven't got any specifics (I'm away from the dev machine at the moment and I can't remember specifically the build error), but is there a general overview somewhere about how to reliably copy all required dependencies for my tests project such that NCrunch can use them?

I've browsed the wiki and found a few articles, but I couldn't get those to work (most likely my fault).

Also, can we control the architecture of NCrunch tests? I always want them running in x86.

Cheers.
When I have NCrunch in its default configuration, I get the following error on a test that attempts to use the SQL CE managed DLL:

SetUp :
System.Data.SqlServerCe.SqlCeException :
Unable to load the native components of SQL Server Compact corresponding to the ADO.NET provider of version 8854.
Install the correct version of SQL Server Compact. Refer to KB article 974247 for more details.

When I debug the test, I query the
System.AppDomain.CurrentDomain.BaseDirectory
for the working directory and get a similar folder path:

C:\Users\RaringCoder\AppData\Local\NCrunch\4764\5\MyApp\bin\Debug

This only contains the DLL of the MyApp.Tests project (where all of my unit tests are located). It doesn't contain any of the references used by the project. Hopefully this is enough information.

Edited

Hi, thanks for posting!

If you haven't read it yet, there's a page in the wiki that describes some common problems that can cause test related issues like the one you've described - https://www.ncrunch.net/documentation/troubleshooting_test-and-runtime-related-issues

I have a feeling that the library you're using is incompatible with the way NCrunch builds its workspace, so turning on the 'Copy referenced assemblies to workspace' would be the first thing I would try.

You can also force the test runner to run as x86 through your NCrunch project level configuration - though it will use this by default unless you have the assembly set to build as x64. Most likely you're already running as x86.

Let me know if the configuration setting above solves your problem, if not, we'll try something else :)


Cheers,

Remco
Hi Remco,

I tried setting that configuration to copy references on just the test project and set the solution-wide architecture to x86. I then did a resync, rebuild, re-run all and still get the same error unfortunately.
Do you have any resource files that the SQL dependency makes use of but aren't included in your project file? You can quickly identify if this is the cause through the following:

1. Right click on the failed test in the tests window
2. Choose Advanced->Browse to workspace
3. Open an explorer window to your test project directory
4. Compare the files that exist between the explorer windows. Check specifically for resource files, binaries and non source files (since if source files were missing, your build will have failed)

If you have any files missing that are being used by the SQL dependency, you can add them using the 'Additional files to include' option under your NCrunch project level configuration.

Does this solve the issue?
Also - something else I forgot to mention .. if you haven't already, try turning on that 'Copy referenced assemblies to workspace' for ALL of your projects (not just the test project). Sometimes this makes a difference as the dependency may be deeper down the project tree.
Remco wrote:Do you have any resource files that the SQL dependency makes use of but aren't included in your project file? You can quickly identify if this is the cause through the following:

1. Right click on the failed test in the tests window
2. Choose Advanced->Browse to workspace
3. Open an explorer window to your test project directory
4. Compare the files that exist between the explorer windows. Check specifically for resource files, binaries and non source files (since if source files were missing, your build will have failed)

If you have any files missing that are being used by the SQL dependency, you can add them using the 'Additional files to include' option under your NCrunch project level configuration.

Does this solve the issue?

Yeah this helps. My tests project correctly pulls in the native binaries for SQL CE. NCrunch doesn't copy those, I presume because they are referenced by the SQL CE managed DLL as opposed to directly from my project.

If I manually copy those files into the bin\debug of the workspace (after setting "Copy referenced assemblies to workspace" to true for all projects), the NCrunch tests start working.

The folder structure of where these DLLs are located is dictated by Nuget. The "packages" folder is a sibling of the "MyApp.Tests" project folder.

I need to include the folders and files under this root directory:

packages\SqlServerCompact.4.0.8852.1\NativeBinaries

Adding additional files takes a path. Can you provide an example of how I might add these files?
The best option would be to include these packages in your 'Additional files to include' at solution level.

The expression for this would be done as relative to your solution file, and you can use **.* to make sure the entire directory structure is included. For example:

packages\SqlServerCompact.4.0.8852.1\**.*
Hi Remco,

That didn't work.

However, what did work was having NCrunch run the post-build step. This was added by Nuget to my project without me realising (I checked after that setting fixed NCrunch).

I presume this was required because you copy the entire project into the NCrunch area and compile it again? I was mistakenly assuming that you just copy what was output from my project bin.

Thanks for your help, working wonderfully now.

Adam
Hi Adam,

Interesting that Nuget was using a post build step - this doesn't match any of my previous experiences in testing NCrunch with Nuget setup. I'm glad you managed to find a solution, though are you able to share any details on the version of Nuget that you're using? Ideally NCrunch should be handling this situation automatically.

You are correct in your understanding that NCrunch shadow copies the entire project :)


Thanks!

Remco
I'm currently using Nuget 1.5.20902.9026, though I don't think issue is with Nuget itself. I was referring to the package for SQL CE 4; when I installed it I believe the package itself configured the post-build step. The post-build takes the native binaries that are found in the SQL CE packages area and copies them in, as in VS you only reference the managed DLL.

The plot thickens slightly, I also need to copy the packages directory using the additional files feature, this is because the post build step looks relatively for the files. I also need to enable "copy references to workspace".

So in summary, all the points you initially stated were required along with enabling post-build steps.

Now I just need to address the speed of the unit tests because it creates a SQL CE database file each time... :-)

Cheers,
Adam

Edited

Thanks for the info - I'll have a bit of a think about this one, as it seems quite SQL-CE specific .. maybe I'll add a warning for when the project has a pre/post build step that NCrunch has deactivated.
I ran into this problem today running our integration tests. Not on my local machine though, but on my NCrunch Grid Server. This thread provided some help. Let me expand a bit on the solution, in case anyone else finds this.

When you install SQL Server CE via Nuget to a project, it adds the following post-build step to the project in question:

if not exist "$(TargetDir)x86" md "$(TargetDir)x86"
xcopy /s /y "$(SolutionDir)packages\Microsoft.SqlServer.Compact.4.0.8876.1\NativeBinaries\x86\*.*" "$(TargetDir)x86"
if not exist "$(TargetDir)amd64" md "$(TargetDir)amd64"
xcopy /s /y "$(SolutionDir)packages\Microsoft.SqlServer.Compact.4.0.8876.1\NativeBinaries\amd64\*.*" "$(TargetDir)amd64"


This fails because the $(SolutionDir) is not resolved to the "correct" place. In order to get everything to work on the grid server, I had to take these steps:

[list=1]
  • Activate "Copy referenced assemblies to workspace" for the project in question.
  • Activate "Run post-build events" for the project in question.
  • Add "..\packages\Microsoft.SqlServer.Compact.*\NativeBinaries\**.*" to "Additional files to include" for the project in question. The star in the middle is just to avoid hardcoding a specific version number.

  • (I also had to check in the ".ncrunchproject" file, something I had no need to do before.)

    Post a reply

    Log in to reply.