ABAP Unit question
Hello,
I am new to ABAP unit, and not really sure on how I can execute one ABAP unit test script.
Let's say I have the below code (extracted from SAPBC401_PRJD_ABAP_UNIT program in SAP ECC 6.0 system (SP9).
**& Report SAPBC401_PRJD_ABAP_UNIT
REPORT sapbc401_prjd_abap_unit.
CLASS myclass DEFINITION.
PUBLIC SECTION.
CLASS-DATA text TYPE string.
CLASS-METHODS set_text_to_x.
ENDCLASS.
CLASS myclass IMPLEMENTATION.
METHOD set_text_to_x.
text = 'U'.
ENDMETHOD.
ENDCLASS.
* Test classes
CLASS mytest DEFINITION FOR TESTING.
PRIVATE SECTION.
METHODS mytestmethod FOR TESTING.
ENDCLASS.
CLASS mytest IMPLEMENTATION.
METHOD mytestmethod.
myclass=>set_text_to_x( ).
cl_aunit_assert=>assert_equals( act = myclass=>text
exp = 'X' ).
ENDMETHOD.
ENDCLASS.
The simple question is, how I can execute the
mytestmethod
from
mytest
class?
If I just execute the above code using F8, I believe nothing is executed right?
I tried to append the below code, but it is also only executing the method in that particular class (not in the test case class).
start-of-selection.
myclass=>set_text_to_x( ).
Please help me to start...
Thanks in advanced.
rgs,
hiroshi
Dear Klaus,
Thanks very much! The link is very useful.
I implemented the below class for test, and now I got the idea how to continue!
class ZCL_WALLET definition
public
final
create public .
*"* public components of class ZCL_WALLET
*"* do not include other source files here!!!
public section.
data LIQUIDITY type I .
methods CHECK_LIQUIDITY .
methods GET_LIQUIDITY
returning
value(RE_LIQUIDITY) type STRING .
methods PUT_IN
importing
!EUROS type I .
REPORT Z_MAIN_ABAPUNIT.
class lcl_Wallet_Test definition for testing. "#AU Risk_Level Harmless
private section.
methods check_Liquidity for testing.
endclass.
class lcl_Wallet_Test implementation.
method check_Liquidity.
data:
my_Wallet type ref to zcl_Wallet,
liquidity type i.
create object my_Wallet.
liquidity = my_Wallet->get_Liquidity( ).
cl_Aunit_Assert=>assert_Equals(
act = liquidity
exp = 1 "purposely put 1 to show error
msg = 'New wallet assumed empty' ).
my_Wallet->put_In( euros = 12 ).
cl_Aunit_Assert=>assert_Equals(
act = my_Wallet->liquidity
exp = 12
msg = 'As many euros as put in before' ).
endmethod.
endclass.
Similar Messages
-
I'm familiar with the technical concept of ABAP Unit testing, using both local or global classes, running sets of Unit Tests using the Code Inspector. However, I'm struggling with two issues:
a) Technical issue: how can I test a Protected or Private method of a class using ABAP Unit testing? It works if the class to be tested has granted friendship to the unit test class, but is that the way to do this?
b) conceptual issue on the types of Unit Tests to define: the Unit Test is based on comparing actual results with expected results. But how can I predict a result on a development environment that is constantly changing?
Thanx in advance
Ben MeijsHi!
For having plan vs actual report, first of all you need to define CO versions through ORKE for your operating concern. You also needs to define plan version through KE14. Also you need to assign COPA reciever plan version for integrated planning.
All your SD condition types need to be assigned to value fields. Also to flow actual values of production orders, CO Orders - it is required to assign PA transfer structure to settlement profile. Settlement profile must allow profitlability segments as settlement recievers and it must be stored in settlement profile. The assignment ensures that variances also flows to COPA. Key figure should also be defined for when assessment is caried out from cost center accounting to COPA. Also your value fields are also to be assigned to Cost component structure in COPA.
After doing above necessary activity, you may plan for your form of report through KE34. Lines, form and header are specified by means of characteristics or value fields.
Hope it helps a lot!
Please revert back for any query.
Thanks & Regards,
Taral Patel -
Hi Experts, questions about abap unit test(abap oo)?
Hi Experts,
I am learning abap unit test, could you please tell me some info about how I can test the performance of some specific code in my program? can abap unit test do this kind of job? and could you please tell me what I can do using abap unit test? and some classic examples are appreciated, thanks in advance!
Kind regards
DawsonHi
Below documents will give you more information,
http://help.sap.com/saphelp_nw04/helpdata/en/a2/8a1b602e858645b8aac1559b638ea4/content.htm
ABAP Testing and Troubleshooting [original link is broken]
http://bi.offis.de/publicProject/g16cglafhqva.htm
regards
Nagaraju -
Hi,
I want to do a unit test for Fm ,but the FM input have some value that always changing and other
Are stay the same ,and the FM output change accordingly , i can build a unit test for this kind of FM,
Or it's worthless ?
Regards
Ricardo AronaHi Naimesh,
But your FM involves some kind of processing i.e. collecting table, modifying data in to internal table, filling up final table etc than you should write ABAP Unit only on that Subroutine.
Yea , my FM get always different key.
the WORKITEM_ID is unique and always changing.
like :
Import
WORKITEM_ID - 005104574
DECISION_KEY - 0001
USER_ID - C0267195
OPERATION_ID - EXEC
so the export data is always changing accordingly .
What u suggest for this kind of issue?
Regards -
Here we go again on questions regarding ABAPUnit. We are trying to use Abap unit within a Class object. All the examples we have seen have to do with Classes and Methods within a Report program.
What we have created:
Class: ZCL_MAIN
Type: General Class
Method: test_abapunit
Class: ZCL_MAIN_TEST
Class type: Abap Unit
Sub Class of ZCL_MAIN
Method: Test_abapunit_test
We try and run the Abap Unit Test from the Test Class and it says it is complete but does not execute our code. I know we are doing something wrong but are stumped as to what it is.
GlennHi Glenn,
although Uwe and Klaus actually provided for the relevant information we too faced some difficulties at first understanding 'where' you actually put the test classes. So here are the mechanics:
Open SE80 or SE24 and implement your class ZCL_MAIN.
While this class is opened, choose <i>Goto->Class-local types->Local Class Definitions/Types</i> from the menu. An editor for local classes is opened on the right.
Type in your class definition ZCL_MAIN_TEST like this:
CLASS lcl_main_test DEFINITION FOR TESTING.
"#AU Risk_Level Harmless
PRIVATE SECTION.
CONSTANTS: some_initial_test_value TYPE i VALUE 5.
DATA: main_class TYPE REF TO zcl_main.
METHODS setup FOR TESTING.
METHODS teardown FOR TESTING.
METHODS check_get_my_int FOR TESTING.
METHODS check_.. FOR TESTING.
ENDCLASS.
This is btw a 'normal' local class, no subclass of cl_abap_unit.. whatsoever.
Go back (menu <i>Goto->Class Definition</i>).
Choose <i>Goto->Class-local types->local class implementations</i> from the menu.
Type in your class implementation for ZCL_MAIN_TEST, i.e. implement especially the setup()-method for the test fixture and the various test-methods.
Compile/activate your class ZCL_MAIN.
Run the test by choosing <i>Test->Unit Test</i> from the context menu of the class ZCL_MAIN.
Hope this helps, regards,
Sebastian -
Transport of ABAP Unit Test Classes between systems.
Hi guys
I have a bit of a dilemma on hands here pertaining to the transport of ABAP Unit test classes. Generally when you create a transport containing classes the transport manager will automatically include all "programs" related to the class like the local type definitions, local implementations and with ABAP Unit the local test classes.
<b>The question that I have is how would one go about excluding the ABAP Unit test classes in transports to production systems and pre-WAS 6.40 systems that do not support ABAP Unit? Does the transport system automatically manage this for you?</b> Any help ideas regarding this would be greatly appreciated.
Kind regards
Ettienne HugoHello Ettienne
I have a very similar problem that I have to maintain objects for release 6.20 and 6.40. Since I appreciate ABAP Unit Tests as being very helpful I do not want to miss them even in such a situation.
My approach is as following:
- If possible, I try to separate the local classes from the tested object, for example:
- Report or function group
- contains an include like <z_include_ft01>
- Within <i><z_include_ft01></i> I have a single stament <i><z_include_au01></i>
The actual ABAP Unit Tests are coded in <z_include_au01>. Now if I need to ship the development to a release < 6.40 the only thing I need to do is to comment the single INCLUDE statement in <z_include_ft01>. This manual step cannot be ommitted (however, it would be simple task to define an eCATT doing this). In addition, include <z_include_au01> should not be in the transport request for the development (if it happened the import error would not affect the function of the development because it is no longer linked together).
Advantage of this approach:
- I clearly see that my development contains ABAP Unit Tests (use naming conventions for the includes) and
- I can ship - with little effort - the development to SAP releases not supporting ABAP Unit Testing yet
Regards
Uwe -
ABAP unit: test classes part of prod. code
Hi all,
I've read <a href="https://weblogs.sdn.sap.com/pub/u/266">Thomas Weiss</a>'s weblog: <a href="/people/thomas.weiss/blog/2004/12/17/a-spotlight-on-abap-unit-part-1 Spotlight on ABAP Unit Part 1</a>.
I posted a question there, but apparently Thomas can't answer at the moment, and since I'm impatient for getting someone's opinion, I reckon we can discuss that topic here :^)
As explained in the <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/c1be1003-0701-0010-3795-f87160de6483">NetWeaver Developers Guide Using ABAP</a>, the test classes are part of the program under test (PUT):
<i>In ABAP Unit, test classes are part of the production code of the TU. This avoids problems arising from the test code being separate from the production code:
- Programs and tests must be kept synchronized.
- You have to ensure that the test and program code are transported together.
- External test code only enables black box tests with an outside perspective of the tested program.
Since the test code is part of the production code, it is easy to keep the unit tests and the
production code up to date if the latter is changed.
Although the test code and the production code are transported through the system landscape, ABAP unit tests do not increase the load on the production system. By default, the test code is not compiled in the production system. Therefore, the test code can never be executed in the production system.</i>
On the other hand, this idea is quite different from what suggested by other unit testing frameworks.
For instance, the documentation of <a href="http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html">Test::Unit - Ruby Unit Testing Framework</a> reads:
<i>Its handy to collect a bunch of related tests, each test represented by a method, into a common test class that knows how to run them.
The tests will be in a separate class from the code theyre testing for a couple of reasons. First of all, it allows your code to stay uncluttered with test code, making it easier to maintain. Second, it allows the tests to be stripped out for deployment, since theyre really there for you, the developer, and your users dont need them. Third, and most importantly, it allows you to set up a common test fixture for your tests to run against.</i>
Regarding the advantages outlined by NetWeaver Developers Guide:
- Programs and tests might be kept synchronized even if they don't belong to the same unit of code.
- Moreover, you could assure that the test and program code are transported together by saving them in the same package.
- And quite frankly, I don't have a thourough understanding of the reason why external tests only allow <i>black box tests with an outside perspective</i>.
What are your opinions on this?
Regards, DavideHi,
when I started unit testing with JUnit I was quite surprised that SAP groups the tests with the production code. But now I actually really like it. It has a few advantages:
- Good tests serve as an excellent documentation so why not bundling them together.
- If you want to look at the tests they are easy to find.
- I write all my class tests as local classes. I found out that it really helps me to focus on writing tests only for the class under test. Sometimes it is quite easy to forget the "unit" and write integration tests again.
Regarding your question:
>And quite frankly, I don't have a thourough understanding of the reason why external tests only allow black box tests with an outside perspective.
Usually I would not use the term black box testing in this context. Maybe you mean that it is easy to access private attributes, methods when you group class and testclass together?
Normally black box testing means that you cannot look at the implementation of the code under test. So your tests are based on the specifications or if there is not one, on your common sense.
cheers
Thomas -
Code Inspector & ABAP UNIT Testclasses
Hallo all,
I am currently trying to implement automated checks using the code inspector. I especially want to see if all our developers use our naming conventions. Also, we use ABAP Unit tests. Unfortunately, the generated test classes are not compliant with our name conventions (And also these Test classes are not interesting to be checked).
Now, I receive many warnings, mostly from the Unit test implementations.
Now: Is there a way to exclude these local test classes from my object set?
Thanks for you help!
Best regards,
Martin ImmeOther language NUnit test frameworks similar to ABAP Unit (AUnit) commonly have project extensions for storing test results. These are useful for the unit level regression (did a new change break any existing functions). History of test results, help narrow down the nature of a current failure by answering the question of 'When did this break?' or when did it last pass? In systems dependent upon outside components the history can provide a pattern for occasional failures due to factors outside the system under test.
I see that ABAP unit test results can be added into the Code Inspector under the Check Variant.
1. Is it common or recomended to use the Code Inspector (SCI) to store AUnit test results?
2. Is it common or recomended to use Code Inspector Object variants to collect individual AUnit tests for a regresssion style 'TestSuite'?
3. What reporting or tools exists for Code Inspector history?
4. Is Application Logging the better spot for AUnit results history?
(also posted on the wiki.. apologies - I'm new to the forum and wiki)
Will Loew-Blosser -
ABAP Unit for Function Module(Function Group)
Hi, Gurus:
Can we use ABAP Unit to test Function Module(Function Group).
If can, give me a simple example. how to create methods. Thanks.
Regards,I'm a little unclear about your question, Yunfa.
Do you want to single-test a SE37 function-module? This can be easily done, just hit the F8 button, and it takes you to a single-test environment.
Do you want to test an FM using an ABAP-program? This too is easy to do. To code the FM-call, there's a button called Pattern, in the standard SE38 screen, where you can put in your FM name, and it inserts the relevant code in your program.
Note that if you're testing BAPIs using the single-test environment, the actual document posting will not happen, because that requires a BAPI_COMMIT_WORK call. So, the way to test BAPIs which post documents would be to write an SE38 program, which also calls the commit-bapi.
Hope this answers your question! -
Hi,
I created a small example of testing using ABAP Unit shown below.
But i get a syntax error in German, i translated to English and this is what i get
The reference to a test class (marking with FOR TESTING) is only in test classes possible
How do i resolve this?
CLASS myclass DEFINITION.
PUBLIC SECTION.
CLASS-DATA text TYPE string.
CLASS-METHODS set_text_to_x.
ENDCLASS.
CLASS myclass IMPLEMENTATION.
METHOD set_text_to_x.
text = 'U'.
ENDMETHOD.
ENDCLASS.
Test classes
CLASS mytest DEFINITION FOR TESTING.
PRIVATE SECTION.
METHODS mytest FOR TESTING.
ENDCLASS.
CLASS mytest IMPLEMENTATION .
METHOD mytest .
myclass=>set_text_to_x( ).
cl_aunit_assert=>assert_equals( act = myclass=>text
exp = 'X'
msg = 'failed').
ENDMETHOD.
ENDCLASS.
DATA my_ref TYPE REF TO mytest.
CREATE OBJECT my_ref type mytest.
call METHOD my_ref->mytest.You're almost there. You don't need to create a object reference to your ABAP Unit Test class. To run the ABAP Unit test, you need to do the Program > Test > Unit Test
CLASS myclass DEFINITION.
PUBLIC SECTION.
CLASS-DATA text TYPE string.
CLASS-METHODS set_text_to_x.
ENDCLASS.
CLASS myclass IMPLEMENTATION.
METHOD set_text_to_x.
text = 'U'.
ENDMETHOD.
ENDCLASS.
* Test classes
CLASS mytest DEFINITION FOR TESTING.
PRIVATE SECTION.
METHODS mytest FOR TESTING.
ENDCLASS.
CLASS mytest IMPLEMENTATION .
METHOD mytest .
myclass=>set_text_to_x( ).
cl_aunit_assert=>assert_equals( act = myclass=>text
exp = 'X'
msg = 'failed').
ENDMETHOD.
ENDCLASS.
Check thread Unit Test Problem
Regards,
Naimesh Patel -
ABAP Unite : how to get the generated log
Hi all,
I'm looking for a way to get the log of the execution of a program linked abap unit tests.
Imagine that I'm using se38 transaction, I launch Program >> test >> Unit Test.
It give me a result screen with the result of defined assert.
What I need is a way to do my own program that can access to this result in order to export them to a specific extarnal test management tool.
So, I would like to know the transaction code of the abap unit transation, the table filled by the log and so on ...
Thanks for help,
Kind Regards
MorganHello Morgan,
there is no API to access ABAP Unit results in release 6.40 und 7.00. You will have to wait for 7.10.
Sorry
Klaus
PS: You may try to access Code Inspector Results meanwhile. -
Hi Experts, need an example for abap unit test(ABAP OO)?
Hi Experts,
I want to do abap unit test for the developed programs, could anyone please give me an example program of development(a simple is ok, just use it to test)? and an abap unit test example for the program, what I want is to use object oriented abap, thanks in advance!
Merry Christmas to you all.
Kind regards
DawsonHi
Below documents will give you more information,
http://help.sap.com/saphelp_nw04/helpdata/en/a2/8a1b602e858645b8aac1559b638ea4/content.htm
ABAP Testing and Troubleshooting [original link is broken]
http://bi.offis.de/publicProject/g16cglafhqva.htm
regards
Nagaraju -
ABAP Unit: Can one call a method in the setup/teardown methods?
Hi all,
I'm using ABAP Unit test ClasseS (what's with the odd capitalization BTW?) in the class builder on NW7.0.
In my SETUP method I do not want to do a CREATE OBJECT, but want to use a GET_INSTANCE method of my main class to instantiate my object.
When I execute my unit test however the instance methods all fail with CX_REF_NOT_ASSIGNED - meaning there is no instance m_ref.
OK, fair enough methinks, let me debug and see what's going on.
Surprise: When I set a breakpoint in the SETUP method it all runs fine. Instances are instantiated and instance method tests execute successfully.
So now I'm a bit stuck trying to investigate a problem that disappears everytime you look at it, a bit like trying to see if the light is off when the fridge is closed... Any ideas?
Cheers,
Mike
Edit: Just to clarify, here's some code:
method setup.
m_ref = zcl_myclass=>get_instance( im_key = 'ABC' ).
" when debugging m_ref is always instantiated and test runs successfully!
endmethod. "Setup
method get_some_data.
result = m_ref->get_some_data( ).
" without breakpoint in the setup method, this fails because m_ref is not assigned
endmethod.
Edited by: Mike Pokraka on Jun 23, 2008 11:57 PMNever mind, found problem, schoolboy error:
My class_setup method created test data but the update hadn't completed so my GET_INSTANCE failed in normal execution but worked in debug because of the associated delay. 'commit work' needed an 'and wait' and now it's happy. -
Deactivating ABAP UNIT in Production system
Hi All,
I have gone through the documention of ABAP UNIT for unit testing. In the documents it is mentioned that the Test classes created for Unit Testing are Local Classes and does not make any negative impact on the Production environment Because these classes are Inactive in Production system.
Can anyone please reply to the follwoing:
1) Are the Test classes Inactive by default in production system?
2) If No then what is the procedure for Deactivating Test classes in the production environment?
3) What is the transaction code for checking the status (active/inactive) these classes?
4) Is the menu path Programs->Test-> ABAP_Unit is inactive in production?
Thanks and Regards,
Ashvender1) Unit Test Framework will look for the Type of Client. If your client is Production client, Unit test framework will automatically make them inactive.
The check happens to allow to run Unit test in FM SABP_AU_TEST_PROGRAM_FROM_EU.
3) Unit Test classes will be complied in the production system but you will not be able to exectue them.
When we create Unit test in development, Table TAUNIT_PROG_INFO is being updated (HAS_TESTS = 'X'). But when we transport our objects along with the ABAP Unit in Production client, this table is not being updated.
4) You can try running the ABAP unit by Program > Test > Unit Test in Production, but will give you message "Unit tests are not possible in production clients".
Regards,
Naimesh Patel -
Hi ,
I am new to oops concept but i am able to understand.
Now i have to create some Unit test plans in ABAP.We are using SAP Netwever 7.1 and WEB AS 6.0
I tried from SE24 but i couldn't get the correct path.
Can you please explain me: if i have a Z program then where and how to write number of Unit test classes. And how it will integrate to with our actual program/FM/any object
Thanks
Umashankar.You can create the ABAP Unit class as a local class in your program, FM, or class which will test the functionality of that individual code lines. Once you implement the ABAP Unit, you can then run the Unit Test by main menu > Execute > Unit Test. E.g. from Program, Program > Execute > Unit Test.
Check this previous discussions:
ABAP Unit, how to use
Unit Testing concept
Regards,
Naimesh Patel
Maybe you are looking for
-
I accidently created a playlist sitting at my computer but saved to my iPod. I would like the playlist on my Itunes library so my husband can put the play list on his ipod which is also synced to my/our computer. I tried to export the playlist then i
-
LabVIEW 8.6.1 Project causes slow Open VI Reference
I have an odd problem with "Open VI Reference.vi". Case 1. Open LabVIEW project containing "Main.vi" Open "Main.vi" Run "Main.vi" Execution of "Open VI Reference.vi" take about 2,000ms Case 2. Open "Main.vi" - No LabVIEW project open Run "Main.vi" E
-
Error message when using the Text Tool
I have just uninstalled and reinstalled my Copy of Photoshop CS6 I now find I cannot use the TEXT TOOL - I get the following error message:- Adobe Photoshop CS6 could not complete your request because something prevented the text engine from being in
-
Re: Elements Premier 10 in UK (PAL) I have just 'imported' a video clip from my Sony DV camcorder via the firewire port. It uses tape but is digital (I think) it save the file to disc in AVI format. This is good I know AVI. After I have edited the cl
-
HT1222 Error occurred installing iOs 7.0 & 7.0.2
Hi, I unable update my iphone 5 software to iOs 7.0 (when verify updating, an error occurred installing iOs 7.0.2). Please do advise for the software update. Wish to get your reply. Thanks!