tag:blogger.com,1999:blog-7022755517551007355.post3061345913870771758..comments2024-03-19T05:20:41.704+01:00Comments on Random thoughts on coding and technology: DUnit and TCustomAttributesJordi Corbillahttp://www.blogger.com/profile/15899608947597831029noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7022755517551007355.post-44584172668639157482012-04-11T13:38:54.299+02:002012-04-11T13:38:54.299+02:00Brilliant Stefan. I've just seen the example a...Brilliant Stefan. I've just seen the example and this is what I was trying to achieve!.<br /><br />Thanks!.Jordi Corbillahttps://www.blogger.com/profile/15899608947597831029noreply@blogger.comtag:blogger.com,1999:blog-7022755517551007355.post-49098628684000860002012-04-11T12:20:07.661+02:002012-04-11T12:20:07.661+02:00I just commited an example showing what I meant. A...I just commited an example showing what I meant. As you can see it has exactly the same functionality as your example but it requires much less writing.Stefan Glienkehttps://www.blogger.com/profile/09320735081289619570noreply@blogger.comtag:blogger.com,1999:blog-7022755517551007355.post-84337547009500229182012-04-10T20:57:38.669+02:002012-04-10T20:57:38.669+02:00Thanks Stefan!.
I'm sure that in the end we c...Thanks Stefan!.<br /><br />I'm sure that in the end we can get something really interesting. I'll give a try to your solution and see how can I use it. I'm still thinking of building a lightweight test framework to run test cases using TCustomAttributes as from my point of view I think it is better using the attribute rather than coding a new test. I'm thinking about the login case; imagine we have the Login(user pass) method which it's a simple method but we want to test it against different users. <br />So instead of writing a loop inside the test through all the users, I like the idea of leaving the responsibility to the attribute.<br />Anyway, I'm still thinking on this as I'm trying to make my unit tests as more readable as possible.<br /><br />JordiJordi Corbillahttps://www.blogger.com/profile/15899608947597831029noreply@blogger.comtag:blogger.com,1999:blog-7022755517551007355.post-81347497221749410942012-04-10T20:26:18.279+02:002012-04-10T20:26:18.279+02:00Thanks, glad you like it ;)
With my solution you ...Thanks, glad you like it ;)<br /><br />With my solution you can easily inherit your attribute classes from TestCaseAttribute and override the constructor. Fill FValues with the passed arguments and you are done. Everything else is handled by the framework.<br /><br />That reduces the actual implementation to 4 lines for each attribute (1 SetLength and 3 Assignments) and zero lines inside the test method itself which then just gets some parameters.<br /><br />P.S. Be careful with the types you use in the arguments. Turned out variants are kinda broken: http://qc.embarcadero.com/wc/qcmain.aspx?d=104778Stefan Glienkehttps://www.blogger.com/profile/09320735081289619570noreply@blogger.comtag:blogger.com,1999:blog-7022755517551007355.post-73502669247758312062012-04-10T14:15:03.254+02:002012-04-10T14:15:03.254+02:00Thanks for your comment Stefan, it is always appre...Thanks for your comment Stefan, it is always appreciated. What I can say?, I take my hat off to you. I have reviewed your solution for DSharp.Testing.DUnit and I think it is brilliant.<br />I'm glad that at least we think alike as I wanted to achieve the same behaviour as C Sharp and you were able to mimic it better.<br /><br />I agree with you on the only drawback of using your solution as it relies on the user correctly inputting the semicolon separated values. As per my solution I rely on the TCustomAttribute so there is no misunderstanding as to what values are acceptable within the attribute.<br /><br />JordiJordi Corbillahttps://www.blogger.com/profile/15899608947597831029noreply@blogger.comtag:blogger.com,1999:blog-7022755517551007355.post-73547190366451379692012-04-09T23:26:07.782+02:002012-04-09T23:26:07.782+02:00The problem with your approach is that it requires...The problem with your approach is that it requires putting way to much stuff inside the test method itself like getting out the passed values itself.<br /><br />Also it does not improve DUnit itself which is only able to execute unparameterized methods.<br /><br />I did this while ago when I saw a question for exactly that on SO (http://stackoverflow.com/questions/8999945/can-i-write-parameterized-tests-in-dunit)<br /><br />The only disadvantage of my solution is because attributes in delphi have very restricted kinds of parameters. Like you cannot have arrays as in .Net. So my solution uses a semicolon separated string to pass the arguments. But you can just extend the attribute class with more constructor overloads if you like.<br /><br />That way you can easily write test methods that take parameters and specify the parameters in the attributes as in .Net as you showed at the beginning. And actually it makes the test code itself also a one liner and not a bunch of setup code that you have to write every time.Stefan Glienkehttps://www.blogger.com/profile/09320735081289619570noreply@blogger.com