简体   繁体   English

从单元测试触发UI控件事件

[英]Firing UI control events from a Unit Test

As a beginner to TDD I am trying to write a test that assumes a property has had its value changed on a PropertyGrid (C#, WinForms, .NET 3.5). 作为TDD的初学者,我试图编写一个测试,假定属性在PropertyGrid(C#,WinForms,.NET 3.5)上的值已更改。

Changing a property on an object in a property grid does not fire the event (fair enough, as it's a UI raised event, so I can see why changing the owned object may be invisible to it). 在属性网格中更改对象上的属性不会触发该事件(这很公平,因为它是UI引发的事件,所以我可以看到为什么更改拥有的对象可能对其不可见)。

I also had the same issue with getting an AfterSelect on a TreeView to fire when changing the SelectedNode property. 在更改SelectedNode属性时,在TreeView上触发AfterSelect也会遇到同样的问题。

I could have a function that my unit test can call that simulates the code a UI event would fire, but that would be cluttering up my code, and unless I make it public, I would have to write all my tests in the same project, or even class, of the objects I am testing (again, I see this as clutter). 我可以有一个单元测试可以调用的函数,该函数可以模拟UI事件将触发的代码,但这会使我的代码混乱不堪,除非我将其公开,否则我必须将所有测试写在同一个项目中,甚至是我正在测试的对象的类(再次,我认为这很混乱)。 This seems ugly to me, and would suffer from maintainability problems. 在我看来,这很丑陋,并且会遇到可维护性问题。

Is there a convention to do this sort of UI based unit-testing 是否有约定进行此类基于UI的单元测试

To unit test your code you will need to mock up an object of the UI interface element. 要对代码进行单元测试,您将需要模拟UI界面元素的对象。 There are many tools you can use to do this, and I can't recommend one over another. 您可以使用许多工具来执行此操作,因此我不推荐一个。 There's a good comparison between MoQ and Rhino Mocks here at Phil Haack's blog that I've found useful and might be useful to you. Moha和Rhino Mocks 在Phil Haack的博客上有一个很好的比较,我发现它很有用,也许对您有用。

Anothing thing to consider if you're using TDD is creating an interface to your views will assist in the TDD process. 如果正在使用TDD,则要考虑的另一件事是创建视图的接口,这将有助于TDD流程。 There is a design model for this (probably more than one, but this is one I use) called Model View Presenter (now split into Passive View and Supervisor Controller). 为此有一个设计模型(可能不止一个,但我使用的是一个模型)称为Model View Presenter(现在分为Passive View和Supervisor Controller)。 Following one of these will make your code behind far more testable in the future. 遵循这些规则之一将使您后面的代码将来更可测试。

Also, bear in mind that testing the UI itself cannot be done through unit testing. 另外,请记住,不能通过单元测试来测试UI本身。 A test automation tool as already suggested in another answer will be appropriate for this, but not for unit testing your code. 另一个答案中已经建议的测试自动化工具将适用于此,但不适用于代码的单元测试。

Microsoft has UI Automation built into the .Net Framework. Microsoft在.Net Framework中内置了UI自动化。 You may be able to use this to simulate a user utilising your software in the normal way. 您可能可以使用它来模拟用户以正常方式使用您的软件。

There is an MSDN article " Using UI Automation for Automated Testing which is a good starting point. MSDN上有一篇文章“ 使用UI自动化进行自动测试 ,这是一个很好的起点。

One option I would recommend for its simplicty is to have your UI just call a helper class or method on the firing of the event and unit test that. 我推荐的一个简单选择是让您的UI在事件触发时调用帮助程序类或方法,并对它进行单元测试。 Make sure it (your event handler in the UI) has as little logic as possible and then from there I'm sure you'll know what to do. 确保它(您的UI中的事件处理程序)具有尽可能少的逻辑,然后从那里确定您将要做什么。

It can be pretty difficult to reach 100% coverage in your unit tests. 在单元测试中很难达到100%的覆盖率。 By difficult I mean of course inefficient. 我很难说的是毫无效率。 Even once you get good at something like that it will, in my opinion, probably add more complexity to your code base than your unit test would merit. 我认为,即使您擅长于这种事情,它也可能会给代码库增加比单元测试值得的复杂性。 If you're not sure how to get your logic segmented into a separate class or method, that's another question I would love to help with. 如果您不确定如何将逻辑划分为单独的类或方法,这是我想帮助的另一个问题。

I'll be interested to see what other techniques people have to work with this kind of issue. 我将很感兴趣地了解人们必须使用哪些其他技术来解决此类问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM