c# - How to unit test WinForms with Visual Studio 2013 Ultimate? -
i added tools | extensions , updates: nunit test adapter, nunit test generator, xunit.net runner visual studio 2012 , 2013, unit test generator.
only unit test generator seems give me context menu item allows me right-click form's class constructor , "generate unit test". however, context menu item not appear when right-click method of plain old class in same project. context method not appear if try generate unit tests button click or textbox selection, including if try anywhere inside code of form designer code (e.g. this.button1.click += new system.eventhandler(this.button1_click);
). why that?
also, while "generate unit test" gives me choice of mstest, nunit , xunit, mstest works. other 2 choices create new test project not test class , generate long error messagebox:
no exports found match constraint:
contractname
nuget.visualstudio.ivspackageinstallerservices
requiredtypeidentity
nuget.visualstudio.ivspackageinstallerservices
finally, skeleton mstest method generated shows no obvious way access form , controls. author of article http://www.steveandrews.me/blog/2008/02/05/testing-the-winforms-ui, using different visual studio in 2008, writes generated method includes code helpfully provides access form:
`[testmethod()] [deploymentitem("windowsformsapplication1.exe")] public void initializecomponenttest() { form1_accessor target = new form1_accessor(); // todo: initialize appropriate value target.initializecomponent(); assert.inconclusive("a method not return value cannot verified."); }`
now, visual studio 2013 ultimate , mstest, is:
[testmethod()] public void form1test() { throw new notimplementedexception(); }
i read elsewhere 1 should use mvvm pattern instead if 1 wants test things textbox selections , button clicks, etc want know if there "proper" way unit test classic winforms inside visual studio. (i'm new this!)
thank you
unit testing private methods bad practice, making classes internal , using internalsvisibleto
attribute can test it.
basically, if have private method, method represents implementation detail of class -- method irrelevant testing efforts. should testing public api of class.
"well, want test logic that's buried deep in private method, it's hard pass data in public api hits of logic in private methods!", might saying. common response advice gave, , there's answer:
logic that's hard test via public api indicator class poorly designed. you'll want start decompose class multiple, smaller classes , test each of those classes through their public api.
now, 1 last thing: you're trying test windows forms. winforms ancient , not make cursory attempt guide down path of using modern patterns , practices, separation of concerns. means have lot of view logic mixed in business logic, makes harder unit test. can starting implement mvp pattern.
Comments
Post a Comment