Visual Studio Unit Testing of Windows Forms

后端 未结 8 2080
既然无缘
既然无缘 2020-12-28 09:09

We\'re working on a project here in Visual Studio 2008. We\'re using the built-in testing suite provided with it (the Microsoft.VisualStudio.TestTools.UnitTesting namespace

相关标签:
8条回答
  • 2020-12-28 09:47

    You would need to refactor the UI so that UI does not need any unit testing. UI should contain minimum or no business logic. There are many patterns that deal with this issue. Martin Fowler has a very good article that explains a lot about these patterns: http://martinfowler.com/eaaDev/uiArchs.html

    There is a small chapter in the Refactoring book by Martin Fowler that talks about refactoring the non testable UI. You could also read Working Effectively With Legacy Code.

    Note: There are tool that could be used to automate the UI testing. SilkTest comes to my mind. But I would not use them if possible.

    0 讨论(0)
  • 2020-12-28 09:52

    It's quite easy to test Winforms with ApprovalTests (www.approvaltests.com or nuget approval tests) and they are compatible with MsTest as well as Nunit.

    There's a video here of how to do it: https://www.youtube.com/watch?v=hKeKBjoSfJ8

    But the process is simple. 1) create the form you want to test in the state you want it verified. 2) call WinFormApprovals.Verify(form)

    ApprovalTests uses the golden master paradigm to screen capture the result. if you like it simply rename the file to .approved and the test will pass.

    The great thing about this for refactoring existing code is you don't even have to worry about what the result is, since you are only concerned that you are not changing it.

    0 讨论(0)
提交回复
热议问题