简体   繁体   English

在 Visual Studio 2013 中运行单元测试时运行其他项目

[英]Run other project when running unit tests in Visual Studio 2013

I have a setup with some VS projects which depend on each other.我有一些相互依赖的 VS 项目的设置。 Simplified, imagine these projects:简化一下,想象一下这些项目:

  1. API Project (ASP.NET WebAPI 2) API 项目(ASP.NET WebAPI 2)
  2. DummyBackendServer (WinForms with a WCF Service) DummyBackendServer(带有 WCF 服务的 WinForms)
  3. API.Test Project (Unit Test Project which should Test API-Project's Controllers) API.Test Project(应测试 API 项目控制器的单元测试项目)

Usually, there would be (for example) an android client, which connects to the API-Server (1) and requests some information.通常,会有(例如)一个 android 客户端,它连接到 API-Server (1) 并请求一些信息。 The API-Server would then connect to the DummyBackendServer (2) to request these information, generate an appropriate format and send an answer to the android client.然后,API 服务器将连接到 DummyBackendServer (2) 以请求这些信息,生成适当的格式并向 android 客户端发送应答。

Now I need to create some unit tests for the API-Server.现在我需要为 API 服务器创建一些单元测试。 My problem is, that I don't find a way to tell VS to start the DummyBackendServer (2), when it runs the unit tests.我的问题是,当它运行单元测试时,我找不到告诉 VS 启动 DummyBackendServer (2) 的方法。 When I first start the DummyServer I can't run the tests, because the menu option is grayed out.当我第一次启动 DummyServer 时,我无法运行测试,因为菜单选项是灰色的。

So, is there any way to tell VS to start another project the tests depend on?那么,有没有办法告诉 VS 启动测试所依赖的另一个项目?

For anyone who doesn't want to unit test the correct way and just want to run multiple projects in the same soluition, here is the answer.对于不想以正确的方式进行单元测试而只想在同一解决方案中运行多个项目的任何人,这里就是答案。

Rightclick the backend project -> Debug -> Start without debugging .右击后端项目 -> Debug -> Start without debugging
The interface will not grey out so you can start other projects.该界面不会变灰,因此您可以启动其他项目。

Start test with rightclick -> Run Tests右键单击开始测试 -> 运行测试
Or run your frontend with debugging as usual by having it set as startup project (Bold in the Solution Explorer) and clicking F5 or the green arrow, Start With Debugging.或者通过将前端设置为启动项目(解决方案资源管理器中的粗体)并单击 F5 或绿色箭头“开始调试”来像往常一样运行调试前端。

Divide and conquer!分而治之!

If the test (some will say that those are not unit test, but that is not part of this question) requires some services to be up - make that happen, Have them deployed to some dev or staging environment.如果测试(有些人会说那些不是单元测试,但这不是这个问题的一部分)需要启动一些服务 - 让它发生,将它们部署到一些开发或暂存环境。 then you only need to configure the connection from the API test assembly.那么你只需要配置来自 API 测试程序集的连接。

I would split the solution in two and call them integration tests.我会将解决方案一分为二,并将它们称为集成测试。 If you want them to bee unit test you have what you need from the post above.如果你想让他们进行蜜蜂单元测试,你可以从上面的帖子中获得你需要的东西。

You should use the IoC containers or something similar in your project, so you can get the mock of your other projects while run the Unit Tests.您应该在您的项目中使用 IoC 容器或类似的东西,这样您就可以在运行单元测试时获得其他项目的mock

Which one you'll select is up to you, personally I use Rhino.Mocks :你会 select 哪一个取决于你,我个人使用Rhino.Mocks

  1. Create a mock repository:创建一个模拟存储库:
MockRepository mocks = new MockRepository();
  1. Add a mock object to the repository:将模拟 object 添加到存储库:
 ISomeInterface robot = (ISomeInterface)mocks.CreateMock(typeof(ISomeInterface));  
 //If you're using C# 2.0, you may use the generic version and avoid upcasting:

 ISomeInterface robot = mocks.CreateMock<ISomeInterface>();
  1. "Record" the methods that you expect to be called on the mock object: “记录”您希望在模拟 object 上调用的方法:
// this method has a return type, so wrap it with Expect.Call
Expect.Call(robot.SendCommand("Wake Up")).Return("Groan");

// this method has void return type, so simply call it
robot.Poke();

//Note that the parameter values provided in these calls represent those values we  
//expect our mock to be called with. Similary, the return value represents the value  
//that the mock will return when this method is called.

//You may expect a method to be called multiple times:

// again, methods that return values use Expect.Call
Expect.Call(robot.SendCommand("Wake Up")).Return("Groan").Repeat.Twice();

// when no return type, any extra information about the method call
// is provided immediately after via static methods on LastCall
robot.Poke();
LastCall.On(robot).Repeat.Twice();
  1. Set the mock object to a "Replay" state where, as called, it will replay the operations just recorded.将模拟 object 设置为“重播” state ,正如所调用的那样,它将重播刚刚记录的操作。
mocks.ReplayAll();
  1. Invoke code that uses the mock object.调用使用模拟 object 的代码。
theButler.GetRobotReady();
  1. Check that all calls were made to the mock object.检查是否所有调用都是针对模拟 object 进行的。
mocks.VerifyAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Visual Studio中运行应用程序时如何运行单元测试 - How to run unit tests when running the application in visual studio 单元测试在Visual Studio中“全部运行”但单独传递时失败 - Unit Tests Fail when “Run All” in Visual Studio but passes individually 无法在visual studio 2013中运行单元测试 - Not able to run unit test in visual studio 2013 Visual Studio 15.8.1 未运行 MS 单元测试 - Visual Studio 15.8.1 not running MS unit tests Visual Studio - 单元测试在项目中加载资源 - Visual Studio - Unit tests loading resources in the project 在Visual Studio 2013中运行Web API项目 - running web api project in visual studio 2013 Visual Studio 2013 没有发现单元测试 - Visual Studio 2013 doesn't discover unit tests 在 Visual Studio/Resharper 中以调试模式运行单元测试时出现“不确定:测试未运行”错误 - “Inconclusive: Test not run” error running unit tests in debug mode in Visual Studio / Resharper C# Visual Studio 单元测试不再在一个存储库中运行,但在另一个存储库中运行:相同的服务应用程序代码 - C# Visual Studio Unit Tests no Longer run in one repo but does in the other: Same Service Application code 在Visual Studio 2013中添加对单元测试项目的引用时出错 - Error when adding a reference to my unit test project in Visual Studio 2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM