1Test::Unit::Assert(3) User Contributed Perl DocumentationTest::Unit::Assert(3)
2
3
4
6 Test::Unit::Assert - unit testing framework assertion class
7
9 # this class is not intended to be used directly,
10 # normally you get the functionality by subclassing from
11 # Test::Unit::TestCase
12
13 use Test::Unit::TestCase;
14
15 # more code here ...
16
17 $self->assert($your_condition_here, $your_optional_message_here);
18
19 # or, for regular expression comparisons:
20 $self->assert(qr/some_pattern/, $result);
21
22 # or, for functional style coderef tests:
23 $self->assert(sub {
24 $_[0] == $_[1]
25 or $self->fail("Expected $_[0], got $_[1]");
26 }, 1, 2);
27
28 # or, for old style regular expression comparisons
29 # (strongly deprecated; see warning below)
30 $self->assert(scalar("foo" =~ /bar/), $your_optional_message_here);
31
32 # Or, if you don't mind us guessing
33 $self->assert_equals('expected', $actual [, $optional_message]);
34 $self->assert_equals(1,$actual);
35 $self->assert_not_equals('not expected', $actual [, $optional_message]);
36 $self->assert_not_equals(0,1);
37
38 # Or, if you want to force the comparator
39 $self->assert_num_equals(1,1);
40 $self->assert_num_not_equals(1,0);
41 $self->assert_str_equals('string','string');
42 $self->assert_str_not_equals('stringA', 'stringB');
43
44 # assert defined/undefined status
45 $self->assert_null(undef);
46 $self->assert_not_null('');
47
49 This class contains the various standard assertions used within the
50 framework. With the exception of the "assert(CODEREF, @ARGS)", all the
51 assertion methods take an optional message after the mandatory fields.
52 The message can either be a single string, or a list, which will get
53 concatenated.
54
55 Although you can specify a message, it is hoped that the default error
56 messages generated when an assertion fails will be good enough for most
57 cases.
58
59 Methods
60 assert_equals(EXPECTED, ACTUAL [, MESSAGE])
61 assert_not_equals(NOTEXPECTED, ACTUAL [, MESSAGE])
62 The catch all assertions of (in)equality. We make a guess about
63 whether to test for numeric or string (in)equality based on the
64 first argument. If it looks like a number then we do a numeric
65 test, if it looks like a string, we do a string test.
66
67 If the first argument is an object, we check to see if the '=='
68 operator has been overloaded and use that if it has, otherwise we
69 do the string test.
70
71 assert_num_equals
72 assert_num_not_equals
73 Force numeric comparison with these two.
74
75 assert_str_equals
76 assert_str_not_equals
77 Force string comparison
78
79 assert_matches(qr/PATTERN/, STRING [, MESSAGE])
80 assert_does_not_match(qr/PATTERN/, STRING [, MESSAGE])
81 Assert that STRING does or does not match the PATTERN regex.
82
83 assert_deep_equals(A, B [, MESSAGE ])
84 Assert that reference A is a deep copy of reference B. The
85 references can be complex, deep structures. If they are different,
86 the default message will display the place where they start
87 differing.
88
89 NOTE This is NOT well-tested on circular references. Nor am I
90 quite sure what will happen with filehandles.
91
92 assert_null(ARG [, MESSAGE])
93 assert_not_null(ARG [, MESSAGE])
94 Assert that ARG is defined or not defined.
95
96 assert(BOOLEAN [, MESSAGE])
97 Checks if the BOOLEAN expression returns a true value that is
98 neither a CODE ref nor a REGEXP. Note that MESSAGE is almost non
99 optional in this case, otherwise all the assertion has to go on is
100 the truth or otherwise of the boolean.
101
102 If you want to use the "old" style for testing regular expression
103 matching, please be aware of this: the arguments to assert() are
104 evaluated in list context, e.g. making a failing regex "pull" the
105 message into the place of the first argument. Since this is usually
106 just plain wrong, please use scalar() to force the regex comparison
107 to yield a useful boolean value.
108
109 assert(qr/PATTERN/, ACTUAL [, MESSAGE])
110 Matches ACTUAL against the PATTERN regex. If you omit MESSAGE, you
111 should get a sensible error message.
112
113 assert(CODEREF, @ARGS)
114 Calls CODEREF->(@ARGS). Assertion fails if this returns false (or
115 throws Test::Unit::Failure)
116
117 assert_raises(EXCEPTION_CLASS, CODEREF [, MESSAGE])
118 Calls CODEREF->(). Assertion fails unless an exception of class
119 EXCEPTION_CLASS is raised.
120
121 multi_assert(ASSERTION, @ARGSETS)
122 Calls $self->assert(ASSERTION, @$ARGSET) for each $ARGSET in
123 @ARGSETS.
124
125 ok(@ARGS)
126 Simulates the behaviour of the Test module. Deprecated.
127
129 Copyright (c) 2000-2002, 2005 the PerlUnit Development Team (see
130 Test::Unit or the AUTHORS file included in this distribution).
131
132 All rights reserved. This program is free software; you can
133 redistribute it and/or modify it under the same terms as Perl itself.
134
136 • Test::Unit::Assertion
137
138 • Test::Unit::Assertion::Regexp
139
140 • Test::Unit::Assertion::CodeRef
141
142 • Test::Unit::Assertion::Boolean
143
144 • Test::Unit::TestCase
145
146 • Test::Unit::Exception
147
148 • The framework self-testing suite (t::tlib::AllTests)
149
150
151
152perl v5.36.0 2023-01-20 Test::Unit::Assert(3)