Cefsharp: Can not run Unit test

Created on 19 Dec 2014  Â·  27Comments  Â·  Source: cefsharp/CefSharp

Hello!
I have created a new Unittest-Project in my solution with some basic Unittests.
But when i want to execute the test nothing happens, it seems that the test is stuck during the initialization.
Do you have any idea why i cant execute my Unittest.

Thanks in advance!

Most helpful comment

There are 2 issues that means the Visual Studio test runner will not work:

  1. the unit test runner is a process that lives in a separate directory to the native CefSharp DLLs. The native CefSharp DLLs must live in the same directory as the unit test runner process.
  2. unit tests are run in separate AppDomains and CefSharp needs to run in the default AppDomain.

The solution I use to work around these issues is to use the NUnit console runner. It is a standalone .exe that you can put alongside the CefSharp files, and it has an option for running tests in the primary AppDomain:

  1. use the NuGet package https://www.nuget.org/packages/NUnit.ConsoleRunner/
  2. in your unit test .csproj include <Link> references to the nunit console runner files and copy them to your output directory, like this:
    <None Include="..\packages\NUnit.ConsoleRunner.3.5.0\tools\nunit3-console.exe">
      <Link>nunit3-console.exe</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  1. the necessary files you need are Mono.Cecil.dll, nunit-agent.exe, nunit-agent.exe.config, nunit.engine.api.dll, nunit.engine.dll, nunit3-console.exe and nunit3-console.exe.config.
  2. run the nunit3 console runner on the command line with the --domain=None --inprocess parameters. e.g.:
    C:\CefSharpTest\bin\Debug\nunit3-console.exe CefSharpTest.dll --domain=None --inprocess
  3. you can also run the tests using the Visual Studio debugger via Unit test project > Properties > Debug > Start external program: C:CefSharpTest\bin\Debugnunit3-console.exe

All 27 comments

What does this have to with cefsharp?

Because in my other solution unit testing works fine

CefSharp cannot be used with test frameworks like xUnit.

See background info here #351

@hpposch Can you confirm which test framework your using?

I am writting normal Init tests in visual studio, i do not usr any framework.

Unit not Init tests

http://msdn.microsoft.com/en-us/library/ms243147%28v=vs.80%29

It's still a Unit Test framework, just so happens to come pre-installed with Visual Studio.

Regardless until #351 is resolved you'll have to use a different approach.

(Closing this as we really only need one issue that represents the problem)

Ok, i thought this is different.

Every unit tests framework that I'm aware of, including the one built into Visual Studio loads their tests in a separate AppDomain which is a known issue for cli c++ libraries, this problem isn't specific to CefSharp.

Yeah, i already read this in the other post.
Thanks for your help!

Is there a way to test my Code?
How do you test your Code?

I think your question may be more suited to a site like StackOverflow as you will get a better range of answers (and also because this is not specific to CefSharp).

I personally use functional tests, where my program runs normally and another thread performs mouse clicks and keyboard entry.

You can also consider writing your CEF unit tests as part of your main program code. For example, you run your unit tests via yourprogram.exe /cefunittest, and simply call a static function that runs the tests. This will solve AppDomain issues. Of course, you will have to ditch the framework for this, and manually program the setup/teardown/result routines.

By the way, i think i found a solution for the problem with the unit tests..
But i have to do a litlle bit more of testing before i can guarantee that my solution works..
If it works i will provide a 'framework' for testing cefsharp..

If it works i will provide a 'framework' for testing cefsharp..

Interesting! Looking forward to hearing more.

I hope i can provide the solution within this month ;)

Are you making code modifications or going another route?

No, i am trying to make it from outside, so that you do not have to modify your code..
Maybe you will have to make an annotation in the code so that the framework can find the part where it can execute code..

But as i said i have to work on it since it is not working perfectly at the moment..

Sounds interesting :+1:

For reference it's not too hard to get the WPF version of CefSharp working with https://github.com/red-gate/RedGate.AppHost

So adapting that for use with a unit testing framework should be pretty easy.

In the past RedGate had used an older WinForms version, so there's probably a way to test WinForms and WPF with a few minor modifications to RedGate.AppHost

From memory the modifications required relate to the way it looks up the client/host.

https://github.com/red-gate/sql-scripts/blob/7e51df7c40d64496bad5f0c42c4585c57fd09d09/RedGate.SSC.Windows.Client/packages.config#L3

For reference it's not too hard to get the WPF version of CefSharp working with https://github.com/red-gate/RedGate.AppHost

I am working on a WPF application, but i am interested in standard Unit Tests..
Because in a normal case i do not have to use my GUI to test if my business-logic works..
Thats why i want to write Unit Tests.!

Because in a normal case i do not have to use my GUI to test if my business-logic works..

As CefSharp is a UI component effectively, weather you see the interface or not it must exist somewhere.

I don't believe you've adequately assessed what RedGate.AppHost is capable of. You can run the UI out of process and provides two way communication. With a few tweaks it's likely an ideal solution for interacting with CefSharp from unit tests, e.g. xUnit

@hpposch Did you manage to get the unit tests working? I'm using the WinForms version of CefSharp and I need unit tests as I'm working on a C# interface of a JavaScript library and there's almost no logic but a lot of duck typing so it's very easy to make a mistake.
However it's possible that I've got a different error – my VisualStudio throws CefSharp.Common will work out of the box if you specify platform. I've chosen to target x64 but the error appears anyway... 😟

However it's possible that I've got a different error – my VisualStudio throws CefSharp.Common will work out of the box if you specify platform. I've chosen to target x64 but the error appears anyway... :worried:

The solution needs to target x64

@m93a I am sorry, but i stopped working on that because i had to work an a new project.
I don't know if i am going to redevelop a solution for that issue.

@merceyz

The solution needs to target x64

This is not my experience. I've managed to have only the executable that launches CefSharp set to x86 or 864.

Still no success 😕
I've got two separate projects – the main one is a WinForms project containing the actual program. Then I've got a separate Unit Test Project with dependency on the main project. I managed to add x86 and x64 options in the Configuration Manager and double-checked that achitectures of the two projects match. I also made sure that Test>Test Settings>Default Architecture is set to the current architecture.

I tried running the tests (Ctrl+R, T) first for x64, then x86 but both of them failed with the same message:

System.IO.FileNotFoundException: Could not load file or assembly CefSharp, Version=57.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138 or one of its dependencies. The specified module could not be found.
File name: CefSharp, Version=57.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138
   in CefSharp.CefSharpApp.OnBeforeChildProcessLaunch(CefSharpApp* , scoped_refptr<CefCommandLine>* commandLine)

I can guarantee NuGet packages cef.redist.x86, cef.redist.x64, CefSharp.Common and CefSharp.WinForms are installed in both projects. The main project works without problems.


EDIT Fusion Viewer logs this:

LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.

LOG: Post-policy reference: CefSharp, Version=57.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138

LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 14.0/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/TESTWINDOW/CefSharp.DLL
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 14.0/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/TESTWINDOW/CefSharp/CefSharp.DLL
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 14.0/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/TESTWINDOW/Extensions/CefSharp.DLL
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 14.0/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/TESTWINDOW/Extensions/CefSharp/CefSharp.DLL
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 14.0/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/TESTWINDOW/CefSharp.EXE
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 14.0/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/TESTWINDOW/CefSharp/CefSharp.EXE
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 14.0/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/TESTWINDOW/Extensions/CefSharp.EXE
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 14.0/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/TESTWINDOW/Extensions/CefSharp/CefSharp.EXE

And as you can guess, there are no CefSharp files in TESTWINDOW/Extensions.

There are 2 issues that means the Visual Studio test runner will not work:

  1. the unit test runner is a process that lives in a separate directory to the native CefSharp DLLs. The native CefSharp DLLs must live in the same directory as the unit test runner process.
  2. unit tests are run in separate AppDomains and CefSharp needs to run in the default AppDomain.

The solution I use to work around these issues is to use the NUnit console runner. It is a standalone .exe that you can put alongside the CefSharp files, and it has an option for running tests in the primary AppDomain:

  1. use the NuGet package https://www.nuget.org/packages/NUnit.ConsoleRunner/
  2. in your unit test .csproj include <Link> references to the nunit console runner files and copy them to your output directory, like this:
    <None Include="..\packages\NUnit.ConsoleRunner.3.5.0\tools\nunit3-console.exe">
      <Link>nunit3-console.exe</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  1. the necessary files you need are Mono.Cecil.dll, nunit-agent.exe, nunit-agent.exe.config, nunit.engine.api.dll, nunit.engine.dll, nunit3-console.exe and nunit3-console.exe.config.
  2. run the nunit3 console runner on the command line with the --domain=None --inprocess parameters. e.g.:
    C:\CefSharpTest\bin\Debug\nunit3-console.exe CefSharpTest.dll --domain=None --inprocess
  3. you can also run the tests using the Visual Studio debugger via Unit test project > Properties > Debug > Start external program: C:CefSharpTest\bin\Debugnunit3-console.exe
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nico87 picture nico87  Â·  46Comments

arvindlloyds picture arvindlloyds  Â·  25Comments

amaitland picture amaitland  Â·  34Comments

joaompneves picture joaompneves  Â·  27Comments

croemheld picture croemheld  Â·  39Comments