/customers/iconara.net/iconara.net/httpd.www/blog/wp-content/plugins/wp-super-cache/wp-cache-phase1.php Warning: Cannot modify header information - headers already sent by (output started at /customers/4/c/9/iconara.net/httpd.www/blog/wp-content/advanced-cache.php:1) in /customers/4/c/9/iconara.net/httpd.www/blog/wp-includes/feed-rss2-comments.php on line 8 Comments on: Writing testable code is writing high quality code http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/ Thu, 05 Jul 2012 13:41:39 +0000 hourly 1 http://wordpress.org/?v=3.0 By: Marcus Stade http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7579 Marcus Stade Wed, 10 Sep 2008 10:11:50 +0000 http://blog.iconara.net/?p=313#comment-7579 <p>@Theo, no, I think you got it right. I was referring to view as in a view class, not the view in the MVC pattern. Maybe component is a better word, but I'd like to differentiate between component as in buttons and more complex components such as grids or other kinds of composites. Anyhow, I think we're very much on the same page, except you are a better writer than me and get your point across much more effectively. Thanks for the links!</p> @Theo, no, I think you got it right. I was referring to view as in a view class, not the view in the MVC pattern. Maybe component is a better word, but I’d like to differentiate between component as in buttons and more complex components such as grids or other kinds of composites. Anyhow, I think we’re very much on the same page, except you are a better writer than me and get your point across much more effectively. Thanks for the links!

]]>
By: Theo http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7578 Theo Wed, 10 Sep 2008 10:02:58 +0000 http://blog.iconara.net/?p=313#comment-7578 <p>@ronen</p> <p>Yes, if you test private methods you do something that "private" is meant to avoid: place dependencies on code that is internal and subject to change. So don't. It should suffice to test the public interface of your unit, i.e. class. I agree that sometimes you would like to write a test for a subunit, like a private or protected method, because it is complex and you would like to make sure that it really does what it's supposed to do, but don't. Refactor your code instead. If that method is so complex or critical and you really need to test it separately, perhaps it would be better to extract it into it's own class (which a suitably abstract interface). In <em>Refactoring</em> Fowler mentions the situation and if I remember correctly the advice is that either it's public and you test it, or you refactor until you can test it (but still maintain the level of information hiding you should have).</p> <p>@Marcus/ronen</p> <p>Some testing isn't practical to do automatically, you will always have to do visual tests -- for example to make sure that, say, a button not only is disabled, but <em>looks</em> disabled. So, consider the situation when you have a presentation model with a property that determines if a button is enabled, and a method that is called when someone clicks the button. You also have a view with the button in question, and it binds the <code>enabled</code> property on the button to the <code>buttonIsEnabled</code> property to the presentation model, and sets up the event handler to call the method when the button is clicked. What part of this setup should you aim to test in automated tests?</p> <ul> <li><p>The state of the presentation model, e.g. manipulating it in such a way that the <code>buttonIsEnabled</code> property should be set to false and checking if that is the case, and also running the related method to see what happens (perhaps it should silently ignore calls when the button is disabled, or throw an exception).</p></li> <li><p>That the view responds to changes in the presentation model, e.g. when the <code>buttonIsEnabled</code> property is false the <code>enabled</code> property on the button is false, etc.</p></li> <li><p>That the button is greyed out when it is disabled and that it is positioned to the right of an input field.</p></li> </ul> <p>I'd say the yes, absolutely, to the first point. I'm very sceptical to the second, it could be done, but I don't see the point. Just because you can doesn't mean you should test everything, some things are too simple to test, like getters and setters (that don't have any complex boundary checking, of course), and this is more or less the same, there's too little to gain from writing the test.</p> <p>To the third point I'd say you're nuts if you think it would be a good test to write, because this is the kind of thing you can only test visually. Autmatic testing of things like layout, positioning, colour, etc. can't give you the level of confidence that you need, because what you measure isn't relevant. The requirements of layout don't call for the kinds of things you can spell out in an automated test.</p> <p>Even though you can write many kinds of tests that test your application at many levels, there are limits to what is practical and what you would gain from testing it automatically. You don't test getters and setters, because the time writing the test doesn't pay itself in terms of your productivity, you can be pretty sure that the setter will work correctly because it is only one line. The same goes for bindings (although there are exceptions) and some other things that are related to the layout and updating of a view.</p> <p>@Marcus</p> <p>From the description in the comment above this I'm not sure you're talking about "view" as in the "V" in "MVC" or "view" as in a view class. Maybe I'm just misinterpreting, and if I am just ignore this. Each view, as in view class (MXML file essentially), should have its own presentation model companion, and if either the view or the presentation model starts getting complex, you refactor and create a number of subviews, all with their own presentation model. The tricky bit with Presentation Model (the pattern) is to get the presentation model objects into the right views the right way. There are different ways of doing this, and it can depend on the application framework you use.</p> <p>It was something else that you said, which I can't seem to find now, that got me thinking about the PAC meta pattern, Presentation-Abstraction-Control. Basically you have a model, a view and a controller in each component, sort of like OpenFlux. It definitely has its uses, especially in component development, where you otherwise easily end up with one fat class that does a little too much. When it comes to crafting views in applications I think that Presentation Model (and the Model-View-Presenter with Application Controller meta pattern) is more suitable.</p> <p>There's a good article on different application architecture meta patterns here: http://ctrl-shift-b.blogspot.com/2007/08/interactive-application-architecture.html</p> @ronen

Yes, if you test private methods you do something that “private” is meant to avoid: place dependencies on code that is internal and subject to change. So don’t. It should suffice to test the public interface of your unit, i.e. class. I agree that sometimes you would like to write a test for a subunit, like a private or protected method, because it is complex and you would like to make sure that it really does what it’s supposed to do, but don’t. Refactor your code instead. If that method is so complex or critical and you really need to test it separately, perhaps it would be better to extract it into it’s own class (which a suitably abstract interface). In Refactoring Fowler mentions the situation and if I remember correctly the advice is that either it’s public and you test it, or you refactor until you can test it (but still maintain the level of information hiding you should have).

@Marcus/ronen

Some testing isn’t practical to do automatically, you will always have to do visual tests — for example to make sure that, say, a button not only is disabled, but looks disabled. So, consider the situation when you have a presentation model with a property that determines if a button is enabled, and a method that is called when someone clicks the button. You also have a view with the button in question, and it binds the enabled property on the button to the buttonIsEnabled property to the presentation model, and sets up the event handler to call the method when the button is clicked. What part of this setup should you aim to test in automated tests?

  • The state of the presentation model, e.g. manipulating it in such a way that the buttonIsEnabled property should be set to false and checking if that is the case, and also running the related method to see what happens (perhaps it should silently ignore calls when the button is disabled, or throw an exception).

  • That the view responds to changes in the presentation model, e.g. when the buttonIsEnabled property is false the enabled property on the button is false, etc.

  • That the button is greyed out when it is disabled and that it is positioned to the right of an input field.

I’d say the yes, absolutely, to the first point. I’m very sceptical to the second, it could be done, but I don’t see the point. Just because you can doesn’t mean you should test everything, some things are too simple to test, like getters and setters (that don’t have any complex boundary checking, of course), and this is more or less the same, there’s too little to gain from writing the test.

To the third point I’d say you’re nuts if you think it would be a good test to write, because this is the kind of thing you can only test visually. Autmatic testing of things like layout, positioning, colour, etc. can’t give you the level of confidence that you need, because what you measure isn’t relevant. The requirements of layout don’t call for the kinds of things you can spell out in an automated test.

Even though you can write many kinds of tests that test your application at many levels, there are limits to what is practical and what you would gain from testing it automatically. You don’t test getters and setters, because the time writing the test doesn’t pay itself in terms of your productivity, you can be pretty sure that the setter will work correctly because it is only one line. The same goes for bindings (although there are exceptions) and some other things that are related to the layout and updating of a view.

@Marcus

From the description in the comment above this I’m not sure you’re talking about “view” as in the “V” in “MVC” or “view” as in a view class. Maybe I’m just misinterpreting, and if I am just ignore this. Each view, as in view class (MXML file essentially), should have its own presentation model companion, and if either the view or the presentation model starts getting complex, you refactor and create a number of subviews, all with their own presentation model. The tricky bit with Presentation Model (the pattern) is to get the presentation model objects into the right views the right way. There are different ways of doing this, and it can depend on the application framework you use.

It was something else that you said, which I can’t seem to find now, that got me thinking about the PAC meta pattern, Presentation-Abstraction-Control. Basically you have a model, a view and a controller in each component, sort of like OpenFlux. It definitely has its uses, especially in component development, where you otherwise easily end up with one fat class that does a little too much. When it comes to crafting views in applications I think that Presentation Model (and the Model-View-Presenter with Application Controller meta pattern) is more suitable.

There’s a good article on different application architecture meta patterns here: http://ctrl-shift-b.blogspot.com/2007/08/interactive-application-architecture.html

]]>
By: Marcus Stade http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7577 Marcus Stade Wed, 10 Sep 2008 08:28:10 +0000 http://blog.iconara.net/?p=313#comment-7577 <p>You're right, it's difficult to test and somewhat reiterates my original question about testing user interfaces. It also raises a question regarding the scalability of the Presentation Model. We've been using a version of the Presentation Model in our projects and quite honestly the model grows fairly easily and quickly along with the complexity of your view. However, in my mind there is little reason for having just one model for a view consisting of several views. In fact I argue that seperating whatever logic and state exists in your sub views from your main view is part of the whole pattern, or at least the pattern which we use. It's aptly illustrated in the "Music Browser" example (http://weblogs.macromedia.com/paulw/archives/presentationpatterns/presentationmodel/demo/PresentationModel.html) where the browser is the main view and the form on the right hand side is a sub view. This is reflected also in the presentation model (right click for source). I'm intruiged as to what unit testing might bring in this scenario to help out with UI testing. Of course, there are still hard to (automatically) test issues revolving the actual visual side of the GUI. Ie, is a button greyed out when it's disabled? However, testing that clicking the button doesn't do anything when it's disabled is still testable.</p> You’re right, it’s difficult to test and somewhat reiterates my original question about testing user interfaces. It also raises a question regarding the scalability of the Presentation Model. We’ve been using a version of the Presentation Model in our projects and quite honestly the model grows fairly easily and quickly along with the complexity of your view. However, in my mind there is little reason for having just one model for a view consisting of several views. In fact I argue that seperating whatever logic and state exists in your sub views from your main view is part of the whole pattern, or at least the pattern which we use. It’s aptly illustrated in the “Music Browser” example (http://weblogs.macromedia.com/paulw/archives/presentationpatterns/presentationmodel/demo/PresentationModel.html) where the browser is the main view and the form on the right hand side is a sub view. This is reflected also in the presentation model (right click for source). I’m intruiged as to what unit testing might bring in this scenario to help out with UI testing. Of course, there are still hard to (automatically) test issues revolving the actual visual side of the GUI. Ie, is a button greyed out when it’s disabled? However, testing that clicking the button doesn’t do anything when it’s disabled is still testable.

]]>
By: ronen http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7573 ronen Tue, 09 Sep 2008 23:10:07 +0000 http://blog.iconara.net/?p=313#comment-7573 <p>Your correct and your describing unit tests, im aiming here for integration tests. Usually you have a model and complex view which is composed out of a number of other components (flex buttons grids what ever), there are concerns that play role with the correct operation of this view that has no direct association to the model or its controller, for example the layout, the styling, the way that events are fired in reaction to user interaction and how they are handled, binding etc.. All these usually don't depend on the model or controller but on the implementation details of the view and have to be validated with tests, im not saying that its easy, its damn hard.</p> Your correct and your describing unit tests, im aiming here for integration tests. Usually you have a model and complex view which is composed out of a number of other components (flex buttons grids what ever), there are concerns that play role with the correct operation of this view that has no direct association to the model or its controller, for example the layout, the styling, the way that events are fired in reaction to user interaction and how they are handled, binding etc.. All these usually don’t depend on the model or controller but on the implementation details of the view and have to be validated with tests, im not saying that its easy, its damn hard.

]]>
By: Marcus Stade http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7572 Marcus Stade Tue, 09 Sep 2008 22:33:37 +0000 http://blog.iconara.net/?p=313#comment-7572 <p>I'm not quite sure I follow. In my mind, a component is a unit itself and should not really care about any other components other than children. Now, the children expose an API which we must assume works whatever way is specified and should not be dependant on the parent (unless specified, which might be a design flaw). That might not always be true, but isn't those kind of issues quite easy to spot (however not always that easy to fix)? At least, that's always been my experience.</p> <p>Considering a component to be a unit itself, it's own model and logic should be quite easy to test, regardless of any integration. Please do correct me if I got you all wrong.</p> <p>System integration however will probably always be an issue, as is diligently mentioned in the Google Testing Blog:</p> <p>"Of course, all the complex issues of integration and system testing remain. Good unit testing gives me a good head start for integration, but I might still be in for unpleasant surprises there."</p> I’m not quite sure I follow. In my mind, a component is a unit itself and should not really care about any other components other than children. Now, the children expose an API which we must assume works whatever way is specified and should not be dependant on the parent (unless specified, which might be a design flaw). That might not always be true, but isn’t those kind of issues quite easy to spot (however not always that easy to fix)? At least, that’s always been my experience.

Considering a component to be a unit itself, it’s own model and logic should be quite easy to test, regardless of any integration. Please do correct me if I got you all wrong.

System integration however will probably always be an issue, as is diligently mentioned in the Google Testing Blog:

“Of course, all the complex issues of integration and system testing remain. Good unit testing gives me a good head start for integration, but I might still be in for unpleasant surprises there.”

]]>
By: ronen http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7571 ronen Tue, 09 Sep 2008 22:04:22 +0000 http://blog.iconara.net/?p=313#comment-7571 <p>The problem is with the integration of widgets (we compose and hook them up into new larger components), when ever there is an integration point between software components there is a potential for bugs. Also when writing custom widgets and firing/processing low level events (like in pure flex) can be hard to manage/refactor without proper test suite.</p> The problem is with the integration of widgets (we compose and hook them up into new larger components), when ever there is an integration point between software components there is a potential for bugs. Also when writing custom widgets and firing/processing low level events (like in pure flex) can be hard to manage/refactor without proper test suite.

]]>
By: Marcus Stade http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7570 Marcus Stade Tue, 09 Sep 2008 21:53:40 +0000 http://blog.iconara.net/?p=313#comment-7570 <p>Maybe widgets are too smart for their own good? In a sense, isn't a ui control a view in and of itself and should follow the mvc pattern as well instead of being a mashup of model, view and controller in one?</p> Maybe widgets are too smart for their own good? In a sense, isn’t a ui control a view in and of itself and should follow the mvc pattern as well instead of being a mashup of model, view and controller in one?

]]>
By: ronen http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7569 ronen Tue, 09 Sep 2008 21:41:45 +0000 http://blog.iconara.net/?p=313#comment-7569 <p>Testable code can conflict with good code, the best example is testing private methods sometimes the only solution is to increase visibility of components in order to make them testable, as for UI testing if your not testing the widgets them self then your doing only half of the work, like it or not widgets contain logic and should be tested (custom ones and compositions of widgets), i enjoy your blog also, keep on the good work!</p> Testable code can conflict with good code, the best example is testing private methods sometimes the only solution is to increase visibility of components in order to make them testable, as for UI testing if your not testing the widgets them self then your doing only half of the work, like it or not widgets contain logic and should be tested (custom ones and compositions of widgets), i enjoy your blog also, keep on the good work!

]]>
By: Marcus Stade http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7568 Marcus Stade Tue, 09 Sep 2008 18:58:16 +0000 http://blog.iconara.net/?p=313#comment-7568 <p>Heh, funny, we've been using the Presentation Model all along except we've been calling it a State Model. It never occurred to me to actually test the model itself. Thanks a lot for your input! Oh, and the blog is great, keep it up!</p> Heh, funny, we’ve been using the Presentation Model all along except we’ve been calling it a State Model. It never occurred to me to actually test the model itself. Thanks a lot for your input! Oh, and the blog is great, keep it up!

]]>
By: Marcus Stade http://blog.iconara.net/2008/09/09/writing-testable-code-is-writing-high-quality-code/comment-page-1/#comment-7567 Marcus Stade Tue, 09 Sep 2008 18:41:48 +0000 http://blog.iconara.net/?p=313#comment-7567 <p>Thanks man, I'll definately check it all out!</p> Thanks man, I’ll definately check it all out!

]]>