在Visual Studio 2015中为UWP加速单元测试的任何方法

时间:2022-05-11 05:31:45

When testing a Windows Universal class library (.NET 4.6) in Visual Studio 2015, the time required in the "red->green->refactor" cycle is quite long. My test project is just the standard MSTest "Unit Test App" project. Even in the simplest scenario of a brand new solution, brand new subject and test projects, and a basic int Add(int n1, int n2) method, it's taking about 8-15 seconds. This is the time (after making a small code change) from clicking "run test" till the pass/fail is shown.

在Visual Studio 2015中测试Windows Universal类库(.NET 4.6)时,“red-> green-> refactor”循环所需的时间非常长。我的测试项目只是标准的MSTest“单元测试应用”项目。即使在全新解决方案,全新主题和测试项目以及基本的int Add(int n1,int n2)方法的最简单场景中,它也需要大约8-15秒。这是单击“运行测试”直到显示通过/失败的时间(在进行小代码更改之后)。

On my machine (Win 10 pro) conducting the same experiment but with a WPF-based solution yields about 1-2 seconds.

在我的机器(Win 10 pro)上进行相同的实验但使用基于WPF的解决方案产生大约1-2秒。

In the UWP scenario, the actual test time itself is listed as 79 ms. The rest of the time is compiling and deploying the unit test app container.

在UWP场景中,实际测试时间本身列为79 ms。其余的时间是编译和部署单元测试应用程序容器。

Is there any way to significantly speed up TDD with UWP?

有没有办法用UWP显着加速TDD?

1 个解决方案

#1


32  

The reason the TDD cycle takes so long is because all UWP code has to run in an AppContainer, and that requires packaging and deployment which is slow.

TDD周期花费这么长时间的原因是因为所有UWP代码都必须在AppContainer中运行,这需要包装和部署很慢。

The way around this problem is to separate as much of your code as you can into a different project that doesn't need to run in an AppContainer and then test that project instead.

解决这个问题的方法是将尽可能多的代码分离到不需要在AppContainer中运行的不同项目中,然后测试该项目。

The solution for this is to use a portable class library for your application logic instead of a UWP class library. You'll find the portable class library in the new project dialog:

解决方案是为您的应用程序逻辑而不是UWP类库使用可移植类库。您将在新项目对话框中找到可移植类库:

在Visual Studio 2015中为UWP加速单元测试的任何方法

The default settings should work:

默认设置应该有效:

在Visual Studio 2015中为UWP加速单元测试的任何方法

You then need to add a reference from the UWP to the portable library so you can consume it. To test the portable library, use a regular non-UWP unit test project:

然后,您需要从UWP添加引用到可移植库,以便您可以使用它。要测试可移植库,请使用常规的非UWP单元测试项目:

在Visual Studio 2015中为UWP加速单元测试的任何方法

Note - to make this work you'll need to change the target framework of the unit test project from 4.5.2 (which is the default) to 4.6.

注意 - 要使这项工作,您需要将单元测试项目的目标框架从4.5.2(默认值)更改为4.6。

Any unit test you run from the regular unit test project will run as fast as possible because it doesn't need to run in the AppContainer. I tested this out and the inner loop speed was great. Hope that helps!

从常规单元测试项目运行的任何单元测试都将尽可能快地运行,因为它不需要在AppContainer中运行。我测试了这个,内循环速度很快。希望有所帮助!

#1


32  

The reason the TDD cycle takes so long is because all UWP code has to run in an AppContainer, and that requires packaging and deployment which is slow.

TDD周期花费这么长时间的原因是因为所有UWP代码都必须在AppContainer中运行,这需要包装和部署很慢。

The way around this problem is to separate as much of your code as you can into a different project that doesn't need to run in an AppContainer and then test that project instead.

解决这个问题的方法是将尽可能多的代码分离到不需要在AppContainer中运行的不同项目中,然后测试该项目。

The solution for this is to use a portable class library for your application logic instead of a UWP class library. You'll find the portable class library in the new project dialog:

解决方案是为您的应用程序逻辑而不是UWP类库使用可移植类库。您将在新项目对话框中找到可移植类库:

在Visual Studio 2015中为UWP加速单元测试的任何方法

The default settings should work:

默认设置应该有效:

在Visual Studio 2015中为UWP加速单元测试的任何方法

You then need to add a reference from the UWP to the portable library so you can consume it. To test the portable library, use a regular non-UWP unit test project:

然后,您需要从UWP添加引用到可移植库,以便您可以使用它。要测试可移植库,请使用常规的非UWP单元测试项目:

在Visual Studio 2015中为UWP加速单元测试的任何方法

Note - to make this work you'll need to change the target framework of the unit test project from 4.5.2 (which is the default) to 4.6.

注意 - 要使这项工作,您需要将单元测试项目的目标框架从4.5.2(默认值)更改为4.6。

Any unit test you run from the regular unit test project will run as fast as possible because it doesn't need to run in the AppContainer. I tested this out and the inner loop speed was great. Hope that helps!

从常规单元测试项目运行的任何单元测试都将尽可能快地运行,因为它不需要在AppContainer中运行。我测试了这个,内循环速度很快。希望有所帮助!