How do you create a unit test assembly for a .NET Portable Class Library?
Asked Answered
S

5

29

I am attempting to unit test a Portable Class Library that I've created and I want to make sure it's being tested with the same framework subset that it targets.

Per the Visual Studio ALM + Team Foundation Server blog, the MSTest unit test framework was converted to a PCL in Visual Studio 2012 RC; however, I am unable to create a portable class library and then reference the MSTest framework in VS2012 RTM.

  • Browsing in the "References" dialog shows me that no unit testing components are registered as compatible with PCL.
  • Adding a manual GAC reference in the project file to Microsoft.VisualStudio.QualityTools.UnitTestFramework yields a reference not found build error.
  • Adding a manual direct assembly reference to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll yields a build warning saying the UnitTestFramework assembly references an incompatible mscorlib version.

I did find (thanks to an early answer) that there is a project type Unit Test Library (Windows Store apps) that references a different MSTest assembly at C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0\ExtensionSDKs\MSTestFramework\11.0\References\CommonConfiguration\neutral\Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll. This project type creates a small no-UI Windows Store app... complete with manifest and everything. It also doesn't allow me to specify which frameworks I'm targeting - it appears to be only for Windows Store apps.

Under the potentially faulty assumption that I should be testing my Portable Class Library projects with unit test assemblies that target the same framework subset as the library-under-test...

How do I create a unit test assembly for a .NET Portable Class Library?

(I am open to other frameworks that also target PCL, I'm just currently unaware of other solutions besides MSTest that have taken this into account.)

Straightaway answered 1/10, 2012 at 16:57 Comment(0)
P
4

xUnit now has support for PCL unit testing; http://xunit.github.io/

Paramecium answered 19/11, 2014 at 13:16 Comment(3)
Can you point me to the documentation showing how to ensure the tests run against the targeted framework subset rather than running against full .NET 4.5 with just a PCL unit test assembly? From what I see, the xunit support is just for writing your test assembly as PCL, not ensuring the runner also executes in that subset... but I could be missing the docs.Straightaway
I now understand better what you're looking for... You are touching on testing the wrong thing though; if the code is PCL compat. it should run the same on each platform. Especially from a unit testing perspective I think you should not care about the platform. However if you're doing integration tests then the platform matters. Then xUnit probably will not help you right now. But look at: github.com/xunit/devices.xunit You might want to contribute there.Paramecium
We have found while maintaining Autofac that the PCL tooling is not infallible. It sometimes can pick the wrong platform subset. Running tests on the actual platform subset would help validate things are working correctly.Straightaway
D
2

As far as i know at this moment there are no PCL libraries for unitesting.

But i found this project on github that seems promising and targets exactly what you need:

https://github.com/jbtule/PclUnit

It follows the same unit test style as Nunit but also borrowed some stuff from XUnit, so it should be fairly easy to accommodate to it.

Didi answered 27/12, 2013 at 17:53 Comment(0)
I
1

Following up on this question recently, I tried to set things up for a new solution and I've got a Portable Class Library which I can unit test now. I cannot remember what exactly I tried before, but here is what I got working right now:

  • Visual Studio Premium 2013 Update 4
  • One project of type Portable Class Library
  • One Windows Forms project
  • One universal app consisting of one Windows, one Windows Phone and a ....Shared project
  • One Unit Test project

The ...Shared project from the universal is empty apart from an App.xaml file, so this is not really being used so far.

All projects containing a UI reference the project of type Portable Class Library, and they all work using the very same code.

The Unit Test project also references the code from the Portable Class Library, I added the reference the usual way (right click on References, Add reference and then choose the PCL project from Solution - Projects.

The solution structure is as shown on the screenshot below:

enter image description here

I'm not entirely sure as to what the problem was when I tried to get the whole PCL unit testing thing working last year, but now it seems work as it should.

Feel free to ask for more information if needed, I'll try to help if possible.

Inae answered 3/8, 2015 at 19:28 Comment(0)
D
0

The portable test library is under the Windows 8 SDK located here (for me):

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0\ExtensionSDKs\MSTestFramework\11.0\References\CommonConfiguration\neutral\Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll

You'll see it has an Assert class and the necessary [TestXXX] attributes. If I create a Portable Library project and add the reference to that assembly, Visual Studio's Test Explorer will show tests marked with the [TestMethod] attribute, but it won't execute them.

Diggs answered 5/10, 2012 at 17:39 Comment(3)
I am not sure how this is helping the enquirer here? If you are trying to ask question, give details and open a new question.Sober
It appears that the "proper" way into this assembly reference is to do File -> New Project -> Windows Store -> Unit Test Library (Windows Store apps). That allows the test runner to execute the tests. Of course, this makes a full "app" with a manifest and everything, which isn't really "portable class library." I'll see if I can find a different way that's more "officially" supported. If not, maybe this is as close to an answer as I get?Straightaway
I updated my question with the above information. I'm really hoping to get an answer where I'm somehow allowed to specify which framework(s) I'm targeting, but maybe that's asking too much. We shall see.Straightaway
S
0

I have a universal app for Windows Store & Windows Phone containing one PCL with my ViewModels. I just created a unit test project and added the viewmodel as a 'linked file'. It is just a small test which checks the string in a hello world application, but it seems to work.

Only time will tell if this solution meets my future needs of testing.

Stammer answered 6/12, 2014 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.