问题
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 incompatiblemscorlib
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.)
回答1:
xUnit now has support for PCL unit testing; http://xunit.github.io/
回答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.
回答3:
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:
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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/12677673/how-do-you-create-a-unit-test-assembly-for-a-net-portable-class-library