开发者

Automatic testing of GUI related private methods

开发者 https://www.devze.com 2022-12-24 12:01 出处:网络
When it comes to GUI programming (at least for web) I feel that often the only thing that would be useful to unit test is some of the private methods*. While unit testing makes perfect sense for back-

When it comes to GUI programming (at least for web) I feel that often the only thing that would be useful to unit test is some of the private methods*. While unit testing makes perfect sense for back-end code, I feel it doesn't quite fit the GUI classes.

What is the best way to add automatic testing of these?


* Why I think the only methods useful to test is private:

Often when I write GUI classes they don't even have any public methods except for the constructor. The public methods if any is trivial, and the c开发者_StackOverflow社区onstructor does most of the job calling private methods.

They receive some data from server does a lot of trivial output and feeds data to the constructor of other classes contained inside it, adding listeners that calls a (more or less directly) calls the server... Most of it pretty trivial (the hardest part is the layout: css, IE, etc.) but sometimes I create some private method that does some advanced tricks, which I definitely do not want to be publicly visible (because it's closely coupled to the implementation of the layout, and likely to change), but is sufficiently complicated to break. These are often only called by the constructor or repeatedly by events in the code, not by any public methods at all.

I'd like to have a way to test this type of methods, without making it public or resorting to reflection trickery.

(BTW: I'm currently using GWT, but I feel this applies to most languages/frameworks I've used when coding for GUI)


Instead of insisting that everything must be private, you should consider adopting one of the many UI-related design patterns, such as MVC, MVP or MVVM.

You UI would delegate all logic to testable UI/Presentation-specific classes with public members. These implementations don't pollute your core Domain Model because they are still UI-specific and can be implemented in separate libraries.

You can unit test such a Presentation library to your heart's content, and also use it to perform Subcutaneous Tests.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号