06-14-2011 12:00 PM
Hi,
How to control the flow of methods in ABAP Unit test. I have a issue following scnario.
I have a set of ABAP Unit test methods. In one method i creating the instance of a class.
Using that same instance i want perform other operations in the rest of the methods.
Since each methods are calling independently instance will not be available in next methods.
Even i tried to call other methods explicitly just after the instance creation, but later after performing all these
rest of the methods will call automatically again.Kindly help to solve this issue.
Thanks & Regads
Brijo.
06-14-2011 4:03 PM
Hi,
Actually tests methods are not supposed to be dependant from other test methods, moreover, you can't rely on the order the test methods are called: it may change in the future. If you need to initialize data, you may use [fixures|http://help.sap.com/saphelp_nw70ehp2/helpdata/en/49/1ec269f3ee6492e10000000a42189b/frameset.htm].
You also may use "normal" methods in your test class.
But remember that unit tests should be [simple|http://www.extremeprogramming.org/rules/simple.html]. If it's not, then you'll certainly need other tools (ECATT for instance)
Regards
Dominique
06-14-2011 5:25 PM
Have you tried the methods SETUP and TEARDOWN in ABAP UNIT, prior to calling your test methods? Also, for me, the tests are executed in the order I have named them in my class definition but then I name like A_something, B_something, etc.
06-14-2011 7:29 PM
You have certain choices:
1. You need to use the fixture method CLASS_SETUP to instantiate your ABAP Unit test. More on [Fixture Methods|http://help.sap.com/saphelp_nw2004s/helpdata/en/ac/381540bf1af72ee10000000a1550b0/frameset.htm]
2. Implement a Singleton Design Pattern in your production class and use this object in each test rather than creating new instance.
3. Design your Test in a such way, that you control the inputs to the next test method. E.g. if your method performs some operation on one of the column in the ITAB, you pass the ITAB, call the method and compare the results with the outcome of the method.
PS - It is true that AU framework is sorting the test methods by its name, but it can be changed to different sequence in future. Thus, you should not design your tests which are based on the the assumption that A_TEST_METHOD will always get executed before B_TEST_METHOD.
Regards,
Naimesh Patel
06-14-2011 9:55 PM
I agree that you should never rely on the order of execution of test cases.
Imagine that your tests will be scheduled for execution based on some criteria like coverage, priority , etc. and you never know which of your methods will be called and in what order.
Good test should produce the result in consistent manner no matter what was executed before/after.
06-23-2011 6:03 AM
Hi,
Thanks for your valuable points. I solved the issue.
Regards,
Brijo.
06-17-2011 5:29 AM
Hi,
Thanks for your valuable points. I solved the issue.
Regards,
Brijo.