public class TestOrg
extends java.lang.Object
public static void main(String[] args) { TestOrg test = new TestOrg("Test description", 2, args); test.finish(); } void testRoutine(TestOrg testParent) { TestOrg test = new TestOrg("Test routine description", 4, testParent); try { result = doSomethingToTest(); test.expect(result == expected, 5, "test case description"); } catch (Exception exc) { test.exception(exc); } test.finish(); }The output for this situation for a well test can be:
================================================== ok Test description ok Test routine description ok test case descriptionor the output is empty. It depends of a calling argument
"---TESTverbose:1"
on any position of main().
If the calling argument is "---TESTverbose:5"
for this example it is outputted:
The output for this situation for a well test can be:================================================== Test routine description ok: test case descriptionIt means the showing output depends on the verbose argument. The output will be written to standard output (System.out). If the output should be more verbose, all test cases are shown. So the tester can see what is tested. If the verbose level is less, only an overview over the test is created.
========================================================================= Test description Test routine description ERROR: test case description @ org.vishia.util.test.Test_TestOrg.mainF(Test_TestOrg.java:45); ...It means the test routine (title) and the failing test case is shown inclusively the line with calling stack of mex. 5 levels. So it may be possible to immediately find the failing reason if it is trivial (which is often so).
expect(CharSequence, CharSequence, int, String)
the position of the first difference can be shown, for example:============================================== Test description Test routine description ERROR: @45 test case description @ org.vishia.... ERRORNested test routines will be supported well. See the examples on org.vishia.util.test.Test_TestOrg.java which is found on https://github.com/JzHartmut/testJava_vishiaBase.
main()
routine in a TestAll
class.Modifier and Type | Field and Description |
---|---|
private boolean |
bOk |
private boolean |
bTitleShown |
private java.util.Formatter |
formatter |
private int |
nTestVerboseLevel |
private int |
nVerboseTitle |
private java.lang.Appendable |
out |
private TestOrg |
parent |
private java.lang.StringBuilder |
sbTxt |
static java.lang.String |
sVersion
Version, history and license.
|
private java.lang.String |
title |
Constructor and Description |
---|
TestOrg(java.lang.String title,
int nVerbose,
java.lang.String[] args)
Ctor stores the title and sets the internal
bOk to true. |
TestOrg(java.lang.String title,
int nVerbose,
java.lang.String[] args,
java.lang.Appendable testout)
Ctor like
TestOrg(String, int, String[]) but with:
Parameter see TestOrg(String, int, String[]) |
TestOrg(java.lang.String title,
int nVerbose,
TestOrg parent)
Ctor for nested tests. stores the title and sets the internal
bOk to true. |
Modifier and Type | Method and Description |
---|---|
void |
exception(java.lang.Exception exc)
Shows a proper text on an non expected exception, sets the internal
bOk to false
Note: An exception aborts the normal execution. |
private void |
expect_(boolean cond,
int nVerbose,
java.lang.String txt,
int nStackLevel,
java.lang.Object... args)
core test routine
|
void |
expect(boolean cond,
int nVerbose,
java.lang.String txt,
java.lang.Object... args)
Simple test check routine
|
int |
expect(java.lang.CharSequence s1,
java.lang.CharSequence s2,
int nVerbose,
java.lang.String txt)
Compares both CharSequences, should be equal.
|
void |
finish()
This should use as last statement in a test routine.
|
boolean |
isOk()
Returns true if this test level has no error, may be invoked before finish for additional action on error.
|
void |
out(java.lang.CharSequence txt,
java.lang.Object... args)
Out to the test output.
|
void |
outln(java.lang.CharSequence txt,
java.lang.Object... args)
Out to the test output with following newline ('\n').
|
private void |
showParentTitle(java.lang.String sPreText) |
public static final java.lang.String sVersion
expect(boolean, int, String, Object...)
, out(CharSequence, Object...)
now with arguemnts
expect(CharSequence, CharSequence, int, String)
now returns the position of difference for evaluation.
expect(CharSequence, CharSequence, int, String)
enhanced with showing positions of difference
private boolean bOk
private final java.lang.String title
private boolean bTitleShown
private final int nTestVerboseLevel
private final java.lang.StringBuilder sbTxt
private final java.util.Formatter formatter
private final int nVerboseTitle
private java.lang.Appendable out
private final TestOrg parent
public TestOrg(java.lang.String title, int nVerbose, java.lang.String[] args)
bOk
to true.
The output is written to System.out (stdout). It calls TestOrg(String, int, String[], Appendable)
.title
- it is stored on shown on finish()
or on an error.nVerbose
- verbose level for the title.
A lower number is a more prior message, a higher (..9) is a message only for a verbose output.
Hence the output is not shown if the basic level (default 4) is lesser.
This is the possibility to hide too much information about "ok" test cases.args
- if one of the args contains "---TESTverbose:4"
where the "4" is a digit between 0 and 9, then all messages with a nVerboseLevel <= this number
are shown also in the ok case as information.
ok messages with verbose level > this level are not shown to prevent to much information about test cases.
The default value is 4.public TestOrg(java.lang.String title, int nVerbose, java.lang.String[] args, java.lang.Appendable testout)
TestOrg(String, int, String[])
but with:
Parameter see TestOrg(String, int, String[])
testout
- any output channel (may be an opened file) for the test output.public TestOrg(java.lang.String title, int nVerbose, TestOrg parent)
bOk
to true.
The settings for the used verbose level and the testout came from parent.title
- it is stored on shown on finish()
or on an error.nVerbose
- verbose level for the title.
A lower number is a more prior message, a higher (..9) is a message only for a verbose output.parent
- from the calling routine. On an error or #finish() the title of all parent
are shown.public int expect(java.lang.CharSequence s1, java.lang.CharSequence s2, int nVerbose, java.lang.String txt)
s1
- The test result text to compare.s2
- The expected text.nVerbose
- level of verbose: Same as in #expect(boolean, int, String)
.
A lower number is a more prior message, a higher (..9) is a message only for a verbose output.txt
- Understandable description of the test casepublic void expect(boolean cond, int nVerbose, java.lang.String txt, java.lang.Object... args)
cond
- if false, bOk
and all parent
.bOk are set to false because the test serie has an error.
If false then "ERROR" on first line position is shown, the txt and 3 stack levels (File and line) are shown
to locate the error in the sources.
If true and nVerbose is <= the given verbose level on constructor (more prior), then then "ok" and the text is shown.nVerbose
- level of verbose:
A lower number is a more prior message, a higher (..9) is a message only for a verbose output.txt
- Understandable description of the test caseprivate void expect_(boolean cond, int nVerbose, java.lang.String txt, int nStackLevel, java.lang.Object... args)
private void showParentTitle(java.lang.String sPreText)
public void exception(java.lang.Exception exc)
bOk
to false
Note: An exception aborts the normal execution. Hence an exception in a test scenario is only practicable
if the exception feature itself should be tested. Then the test routine should be written in a special: boolean bOk = false; try { testRoutineShouldThrow() } catch Exception(exc) { bOk = (exc instanceof Type...); test.expect(exc.getMessage(), expectedMessage,...) } test.expect(bOk, ...)It means this routine is not appropriate to use! This routine is only proper for an unexpected exception. To continue a test though unexected exceptions are possible you should wrap less tests, or only one in a try frame:
try { result = testRoutine(); test.expect(result, expectedResult, ...); } catch Exception(exc) { test.exception(exc); //will be notified as not successfully test. }
exc
- The exception.public void finish()
bOk
). depending of the verbose level.
The verbose level is given on ctor of this. If an error was shown in an expect...(...) routine or in
exception(Exception)
then the title of all test routines are shown already. Hence this routine does nothing.
public boolean isOk()
bOk
of this level.public void out(java.lang.CharSequence txt, java.lang.Object... args)
txt
- public void outln(java.lang.CharSequence txt, java.lang.Object... args)
txt
-