单元测试的WinForms UI [英] Unit testing Winforms UI

查看:128
本文介绍了单元测试的WinForms UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个测试案例我的用户控制哪些会提示使用MessageBox.Show对用户操作要求处理或取消操作。
我如何设计我的单元测试来模拟用户交互继续吗?

I am writing a test case for my User Control which will prompt using MessageBox.Show for User Action asking to process or Cancel the operation. How can I design my unit test to mimic the User interaction to proceed?.

我不希望重构的逻辑移动到中间层。这是获得用户的同意,并与中间层调用程序的一个简单的例子。任何帮助/想法,这种情况下重组UI也将有所帮助。

I do not want to refactor to move the logic to middle tier. This is a simple case of getting User Consent and proceeding with middle tier call. Any help/ideas restructuring UI for this scenario will also be helpful.

推荐答案

点击一个按钮,无非就是调用相应的其他点击事件。所以,你可能想围绕您的测试。

Clicking a button is nothing else than invoking the corresponding click event. So you might want to build your test around that.

甚至更好(如果这是不是这样还),将您的代码从前端的,并围绕企业行为的单元测试,否则你调用通过点击一个按钮。

Even better (if this isn't the case yet), move your code out of the frontend, and build your unittests around the business actions, you'd otherwise invoke by clicking a button.

更新通过笔者的编辑后结果
你不会拿到只要你不准备分裂的事情这个工作,你不能围绕点击这里,点击有你的单元测试。想象一下下面的代码:

update after edit by author
You are not going to get this to work as long as you are not prepared to split things, you cannot build your unit tests around 'click here', 'click there'. Imagine the following code:

private int MyFunction()
{
    bool insideVariable = false;
    if(insideVariable) 
        return 1;
    else
        return 2;
}

您的从不的能够单元测试其中,insideVariable设置为true的情况下;您可以:

You will never be able to unit test the case where insideVariable is set to true; You can either:


  1. 重构代码,让收益1 语句是在什么地方你的中间层

  2. 重构,使收益1 语句是在你的GUI的方法。然后,您可以测试该功能。

  1. Refactor your code so the return 1 statement is somewhere in your middle tier
  2. Refactor so that the return 1 statement is a method in your GUI. You can then test that function.

应用程序前端应该是很容易更换,所以没有业务逻辑应存放在那里。单元测试是只是一个前台旁边住你的主界面。

Application frontends should be quite easily to replace, so no business logic should be stored in there. Unit tests are just another frontend living next to your main GUI.

这篇关于单元测试的WinForms UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆