简体   繁体   中英

Silverlight 5 VS 2012 Unit Tests

For last couple hours i have been trying to generate a Unit Test for a Silverlight application.

A number of posts refer to a "Silverlight Unit Test Project" which is part of the Silverlight Toolkit. But I downloaded the toolkit and still do not have the Test Project, it seems to be only available in VS 2010?

I have added a "Silverlight Class Library" project and added references to:

  1. Microsoft.Silverlight.Testing
  2. Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight

and the following TestClass:

using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTesting
{
    [TestClass]
    public class Class
    {

        [TestMethod]
        public void TestMethod()
        {
            .....    
        }
    }
}

But there are no tests being discovered by Visual Studio 2012 Test Explorer. Even after re-build of solution and restart of application.

Anyone have any ideas? Is this even possible?

This link has the answer that worked for me:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/5e991b0d-8061-4c4e-a17d-82b4abd58d6c/vs-2012-silverlight-unittest

I recommend starting a new Silverlight project and installing the SilverlightToolkit-Testing NuGet package. In your test files, put in usings for Microsoft.Silverlight.Testing and Microsoft.VisualStudio.TestTools.UnitTesting and use regular [TestClass] and [TestMethod] attributes. To run them, you can use the Toolkit test runner by putting RootVisual = UnitTestSystem.CreateTestPage(); in your App.Application_Startup(), use Silverlight Unit Test Adapter (which currently is at v0.0.1 and doesn't really work), or (the best approach by far) install ReSharper and the AgUnit plugin.

To complete this thread,

The Silverlight DLLS are located in C:\\Program Files (x86)\\Microsoft SDKs\\Silverlight\\v5.0\\Toolkit\\dec11\\Testing

I could not get Resharper 7.1 to run the tests but this library helped. You will need to extract using 7-zip so that the DLLS are not blocked. Then restart Visual Studio 2012 and Resharper will run your unit tests.

I believe that you need to install the Silverlight Unit Test Adapter to get the tests to show up in Test Explorer

I was able to run some tests:

  1. Given Visual Studio 2012 Professional(with test runner).

  2. Create class library targeting .NET 4.5 with name like MyProject.Tests .

  3. Reference C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\PublicAssemblies\\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll or from your location.

  4. Add test as usual test for .NET 4.5.

  5. Add project reference to MyProject - project targeting Silverlight 5.

  6. Add some tests. Build. May get error of missing reference: Error 12 The type 'System.Xml.Serialization.IXmlSerializable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' Error 12 The type 'System.Xml.Serialization.IXmlSerializable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'

  7. Reference C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\Silverlight\\v5.0\\System.Xml.dll

  8. Build and get same error. Open *.csproj and ensure hint path: xml <Reference Include="System.Xml"> <HintPath>C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\Silverlight\\v5.0\\System.Xml.dll</HintPath> </Reference>

  9. Run test, eg via right click on TestMethod -> Run Tests . May get error: System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\\Software\\Microsoft\\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\\Software\\Microsoft\\Fusion!EnableLog]. System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\\Software\\Microsoft\\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\\Software\\Microsoft\\Fusion!EnableLog]. Fix is: <Reference Include="System.Windows"> <HintPath>C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\Silverlight\\v5.0\\System.Windows.dll</HintPath> </Reference>

Notes:

  1. Recall that Silverlight 5 assemblies are in the same format as .NET 4.5.
  2. Test fail because .NET 4.5 assemblies are default for project, we need to override via HintPath . I think there may be other way via MSBuild scripts modification and/or assembly binding redirection.
  3. .NET core assemblies are loaded from 4.5, if these differ from Silverlight things may fail. I hope not.
  4. Features depending on Silverlight hosting runtime may fail. Like showing Silverlight window or access HTML DOM. Which is good indicators to refactor code to be Silverlight agnostic. Possible error:


       Test Outcome:    Failed

       Result Message:  
       System.DllNotFoundException: Unable to load DLL 'agcore': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
       Result StackTrace:   
    at MS.Internal.XcpImports.Application_GetCurrentNative(IntPtr context, IntPtr& obj)
       at MS.Internal.XcpImports.Application_GetCurrent(IntPtr& pApp)
       at System.Windows.Application.get_Current()

indicates need to load ActiveX runtime for SL into process.

  1. Referencing Silverlight Toolkit versions of testing assemblies(with [TestMethod] attribute inside) instead of .NET one leads to issue that tests are visible, but not run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM