问题
I need to UnitTest a Class Library (Universal Windows), but from the "Add New Project" windows, I just see this:
This project runs all tests with the App Container, it always opens an App. Since I don't need all that overhead, I would like to know if is there a way to Unit Test just the UWP library.
回答1:
The quick answer is that if you need to test UI related classes you'll need to use this kind of "unit test". However if you have logic which you want to test - extract it to a portable library (PCL) and test it independently using plain old unit testing framework.
回答2:
Now that Portable Class Libraries - now called "Class Library (Legacy Portable)" in Visual Studio - are "legacy", I think the answer to this question has changed slightly.
If you want to write unit tests for non-UI-related logic of your UWP app, pull it out into a .NET Standard Class Library. A UWP app can reference a .NET Standard 2.0 class library without issues.
To actually test a Class Library (.NET Standard) you'll need to create an NUnit/xUnit/MSTest Project (.NET Core) and add a reference to your Class Library (.NET Standard).
I wrote a short blog post on this today once I got my solution working.
回答3:
Add a class library to the project and add the nUnit nuget library and a reference to the class library you want to test
[TestMethod]
public void TestCalculationFruitSmoothie()
{
PointsCalculator2.Core.CalculationHelper helper = new PointsCalculator2.Core.CalculationHelper();
int actual = helper.CalculatePoints(1, 25, 4, 6, false);
int expect = 3;
Assert.AreEqual(expect, actual);
}
来源:https://stackoverflow.com/questions/34123985/is-there-a-way-to-unittest-a-class-library-universal-windows-without-testing-a