Published on :
Macro CREATION_INTERNE

Test of the SASUnit assertMustFail macro

This code is also available in: Deutsch Español Français
Awaiting validation
The script uses the SASUnit unit testing framework to validate the behavior of the `%assertMustFail` macro. It defines a test scenario (`%initScenario`) that contains three distinct test cases (`%initTestcase`). Each test case prepares input data (`work.tst`) and expected data (`work.expected`), then calls the `%assertMustFail` macro in a controlled context. The execution result is then validated using the `%assertLog` (to check for unexpected errors in the log) and `%assertColumns` (to compare the result table `work.tst` to the expected table `work.expected`) macros. The three test cases respectively simulate an assertion that fails, a manual assertion, and an assertion that succeeds, to ensure that `%assertMustFail` reacts correctly and as expected in each situation.
Data Analysis

Type : CREATION_INTERNE


All test data (`work.cas`, `work.scn`, `work.tst`, `work.expected`) are generated and manipulated directly in the script using DATA steps. No external data is read or required.

1 Code Block
DATA STEP Data
Explanation :
Initializes the test scenario with the `%initScenario` macro. Creates the `work.cas` and `work.scn` tables which contain the scenario and test case identifiers, serving as context for the SASUnit framework.
Copied!
1/**
2 \file
3 \ingroup SASUNIT_UTIL
4 
5 \brief Test of macro assertMustFail
6 
7 \version \$Revision$
8 \author \$Author$
9 \date \$Date$
10 \sa \$HeadURL$
11 \copyright Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de
12 This file is part of SASUnit, the Unit testing framework for SAS(R) programs.
13 For copyright information and terms of usage under the GNU Lesser General Public License see included file README.md
14 or https://github.com/HMS-Analytical-Software/SASUnit/wiki/readme/.
15 
16*/ /** \cond */
17 
18%initScenario(i_desc =Test of macro assertMustFail);
19 
20DATA work.cas;
21 cas_scnid=&g_scnid.;
22 cas_id=4;
23 OUTPUT;
24RUN;
25DATA work.scn;
26 scn_scnid=&g_scnid.;
27 OUTPUT;
28RUN;
2 Code Block
Macro Data
Explanation :
Test Case 1: Simulates a call with a failed assertion. It prepares an initial state (`tst_res=2`) and an expected final state (`tst_res=0`). The test then verifies that the execution of `%assertMustFail` produces the expected result and that no errors are generated in the log.
Copied!
1%*** Testcase 1 ***;
2DATA work.tst;
3 tst_scnid=&g_scnid.;
4 tst_casid=4;
5 tst_id=4;
6 tst_res=2;
7 OUTPUT;
8RUN;
9DATA work.expected;
10 SET work.tst;
11 tst_res=0;
12RUN;
13%initTestcase(i_object=assertMustFail.sas, i_desc=call with failed assert);
14 
15/*-- switch to example database -----------------------*/
16%_switch();
17%assertMustFail;
18/*-- switch to real database -----------------------*/
19%_switch();
20 
21%endTestcall;
22 
23%assertLog(i_errors=0,i_warnings=0);
24%assertColumns(i_expected=work.expected, i_actual=work.tst);
25%endTestcase();
3 Code Block
Macro Data
Explanation :
Test Case 2: Simulates a call with a manual assertion. The initial state is `tst_res=1` and the expected result is `tst_res=2`. This test validates the behavior of `%assertMustFail` in this specific configuration.
Copied!
1%*** Testcase 2 ***;
2DATA work.tst;
3 tst_scnid=&g_scnid.;
4 tst_casid=4;
5 tst_id=4;
6 tst_res=1;
7 OUTPUT;
8RUN;
9DATA work.expected;
10 SET work.tst;
11 tst_res=2;
12RUN;
13%initTestcase(i_object=assertMustFail.sas, i_desc=call with manual assert);
14 
15/*-- switch to example database -----------------------*/
16%_switch();
17%assertMustFail;
18/*-- switch to real database -----------------------*/
19%_switch();
20 
21%endTestcall;
22 
23%assertLog(i_errors=0,i_warnings=0);
24%assertColumns(i_expected=work.expected, i_actual=work.tst);
25%endTestcase();
4 Code Block
Macro Data
Explanation :
Test Case 3: Simulates a call with a successful assertion (green assert). `%assertMustFail` expects a failure; a success should therefore be treated as an anomaly by the test framework. The initial state is `tst_res=0` and the expected is `tst_res=2`.
Copied!
1%*** Testcase 3 ***;
2DATA work.tst;
3 tst_scnid=&g_scnid.;
4 tst_casid=4;
5 tst_id=4;
6 tst_res=0;
7 OUTPUT;
8RUN;
9DATA work.expected;
10 SET work.tst;
11 tst_res=2;
12RUN;
13%initTestcase(i_object=assertMustFail.sas, i_desc=call with green assert);
14 
15/*-- switch to example database -----------------------*/
16%_switch();
17%assertMustFail;
18/*-- switch to real database -----------------------*/
19%_switch();
20 
21%endTestcall;
22 
23%assertLog(i_errors=0,i_warnings=0);
24%assertColumns(i_expected=work.expected, i_actual=work.tst);
25%endTestcase();
5 Code Block
Macro
Explanation :
Ends and cleans up the SASUnit test scenario, finalizing all checks.
Copied!
1%endScenario();
2/** \endcond */
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de. This file is part of SASUnit, the Unit testing framework for SAS(R) programs.