scenario.assertions module¶
Assertion methods.
The Assertions
class defines a collection of assertion methods.
- class Assertions¶
Bases:
object
The
Assertions
class gathers static assertion methods.It can be subclasses by classes that onboard these assertion methods, like the base
ScenarioDefinition
andStepDefinition
classes.See the assertion documentation for details.
- static fail(err)¶
Makes the test fail with the given message.
- Parameters:
err – Error message.
- static todo(err)¶
Makes the test fail because it is not completely implemented.
- Parameters:
err – Error message.
- static assertequal(obj1, obj2, err=None, evidence=False)¶
Checks member equality.
- Parameters:
obj1 – First member.
obj2 – Second member.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertnotequal(obj1, obj2, err=None, evidence=False)¶
Checks member inequality.
- Parameters:
obj1 – First member.
obj2 – Second member.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertisnone(obj, err=None, evidence=False)¶
Checks a given value is
None
.- Parameters:
obj – Value expected to be
None
.err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertisnotnone(obj, err=None, evidence=False)¶
Checks a given value is not
None
.- Parameters:
obj – Value expected to be not
None
.err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- Returns:
The value
obj
, ensured not to beNone
.
- static assertisinstance(obj, type, err=None, evidence=False)¶
Checks whether the object is of the given type, or one of the given types.
- Parameters:
obj – Object to check.
type – Type or list of types to check the object against.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- Returns:
The value
obj
, ensured not to be of typetype
.
Note
As it makes the API convenient, we deliberately shadow the built-in with the
type
parameter.
- static assertisnotinstance(obj, type, err=None, evidence=False)¶
Checks whether the object is not of the given type, or none of the given types.
- Parameters:
obj – Object to check.
type – Type or list of types to check the object against.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
Note
As it makes the API convenient, we deliberately shadow the built-in with the
type
parameter.
- static assertsameinstances(obj1, obj2, err=None, evidence=False)¶
Checks two Python instances are the same.
- Parameters:
obj1 – First instance to check.
obj2 – Second instance to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertnotsameinstances(obj1, obj2, err=None, evidence=False)¶
Checks two Python instances are not the same.
- Parameters:
obj1 – First instance to check.
obj2 – Second instance to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static asserttrue(value, err=None, evidence=False)¶
Checks a value is
True
.- Parameters:
value – Value to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertfalse(value, err=None, evidence=False)¶
Checks a value is
False
.- Parameters:
value – Value to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertless(obj1, obj2, err=None, evidence=False)¶
Checks a value is strictly less than another.
- Parameters:
obj1 – Value expected to be below.
obj2 – Value expected to be above.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertlessequal(obj1, obj2, err=None, evidence=False)¶
Checks a value is less than or equal to another.
- Parameters:
obj1 – Value expected to be below.
obj2 – Value expected to be above.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertgreater(obj1, obj2, err=None, evidence=False)¶
Checks a value is strictly greater than another.
- Parameters:
obj1 – Value expected to be above.
obj2 – Value expected to be below.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertgreaterequal(obj1, obj2, err=None, evidence=False)¶
Checks a value is greater than or equal to another.
- Parameters:
obj1 – Value expected to be above.
obj2 – Value expected to be below.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertstrictlybetween(between, low, high, err=None, evidence=False)¶
Checks a value is strictly between two others.
- Parameters:
between – Value expected to be between the others.
low – Low value.
high – High value.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertbetweenorequal(between, low, high, err=None, evidence=False)¶
Checks a value is between or equal to two others.
- Parameters:
between – Value expected to be between the others.
low – Low value.
high – High value.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertnear(obj1, obj2, margin, err=None, evidence=False)¶
Checks a value is near another oe.
- Parameters:
obj1 – Value to check.
obj2 – Reference value.
margin – Margin of error.
err – Optional error message.
evidence –
- Returns:
Evidence activation (see the dedicated note).
- static assertstartswith(string, start, err=None, evidence=False)¶
Checks a string (or bytes) starts with a given pattern
- Parameters:
string – String (or bytes) to check.
start – Expected start pattern.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertnotstartswith(string, start, err=None, evidence=False)¶
Checks a string (or bytes) does not start with a given pattern.
- Parameters:
string – String (or bytes) to check.
start – Unexpected start pattern.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertendswith(string, end, err=None, evidence=False)¶
Checks a string (or bytes) ends with a given pattern.
- Parameters:
string – String (or bytes) to check.
end – Expected end pattern.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertnotendswith(string, end, err=None, evidence=False)¶
Checks a string (or bytes) does not end with a given pattern.
- Parameters:
string – String (or bytes) to check.
end – Unexpected end pattern.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertregex(regex, string, err=None, evidence=False)¶
Checks a string (or bytes) matches a regular expression.
- Parameters:
regex – Regular expression to match with.
string – String (or bytes) to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- Returns:
re
match object.
Note
The
regex
andstring
parameters follow the usual order ofre
functions (contrary tounittest
assertRegex()
).
- static assertnotregex(regex, string, err=None, evidence=False)¶
Checks a string (or bytes) does not match a regular expression.
- Parameters:
regex – Regular expression to match with.
string – String (or bytes) to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
Note
The
regex
andstring
parameters follow the usual order ofre
functions (contrary tounittest
assertNotRegex()
).
- static asserttimeinstep(time, step, err=None, evidence=False, expect_end_time=True)¶
Checks the date/time is within the given step execution times.
- Parameters:
time – Date/time to check.
step – Step specification (see
assertionhelpers.StepExecutionSpecType
).err – Optional error message.
evidence – Evidence activation (see the dedicated note).
expect_end_time –
True
when the step execution is expected to be terminated.
- Returns:
Step execution that matched the specification.
- static asserttimeinsteps(time, start, end, err=None, evidence=False, expect_end_time=True)¶
Checks the date/time is in the execution times of a given range of steps.
- Parameters:
time – Date/time to check.
start – Specification of the first step of the range (see
assertionhelpers.StepExecutionSpecType
).end – Specification of the last step of the range (see
assertionhelpers.StepExecutionSpecType
).err – Optional error message.
evidence – Evidence activation (see the dedicated note).
expect_end_time –
True
when theend
step execution is expected to be terminated.
- Returns:
Step execution that matched the
start
andend
specifications.
- static asserttimebeforestep(time, step, err=None, evidence=False)¶
Checks the date/time is (strictly) before a given step executime time.
- Parameters:
time – Date/time to check.
step – Step specification (see
assertionhelpers.StepExecutionSpecType
).err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- Returns:
Step execution that matched the specification.
- static asserttimeafterstep(time, step, err=None, evidence=False)¶
Checks the date/time is (strictly) after a given step executime time.
- Parameters:
time – Date/time to check.
step – Step specification (see
assertionhelpers.StepExecutionSpecType
).err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- Returns:
Step execution that matched the specification.
- static assertisempty(obj, err=None, evidence=False)¶
Checks that a container object (string, bytes, list, dictionary, set, …) is empty.
- Parameters:
obj – Container object to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertisnotempty(obj, err=None, evidence=False)¶
Checks that a container object (string, bytes, list, dictionary, set, …) is not empty.
- Parameters:
obj – Container object to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertlen(obj, length, err=None, evidence=False)¶
Checks the length of a container object (string, bytes, list, dictionary, set, …).
- Parameters:
obj – Container object which length to check.
length – Expected length.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertin(obj, container, err=None, evidence=False)¶
Checks a pattern or item is in a container object (string, bytes, list, dictionary, set, …).
- Parameters:
obj – Pattern or item to check in
container
.container – Container object.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertnotin(obj, container, err=None, evidence=False)¶
Checks a pattern or item is not in a container object (string, bytes, list, dictionary, set, …).
- Parameters:
obj – Pattern or item to check not in
container
.container – Container object.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertcount(container, obj, count, err=None, evidence=False)¶
Checks a string (or bytes), contains the expected number of patterns, or a list, dictionary or set contains the expected number of a given item.
- Parameters:
container – String (or bytes), list, dictionary or set that should contain
obj
count
times.obj – Pattern or item to check
count
times incontainer
.count – Expected number of
obj
incontainer
.err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertjson(json_data, jsonpath, err=None, evidence=False, type=None, value=None, ref=None, count=1, len=None)¶
Checks JSON content.
- Parameters:
json_data – Input JSON dictionary.
jsonpath –
JSONPath.
Currently a subset of the full syntax (see https://goessner.net/articles/JsonPath/).
err – Optional error message.
evidence – Evidence activation (see
scenario.Assertions
’s documentation).type – Expected type for the matching elements.
value – Expected value for the matching elements.
ref – Reference JSON dictionary giving the expected value for the given path.
count –
Expected number of matching elements.
1 by default. May be set to
None
.len –
Expected length.
It assumes
len()
can be applied on the only searched item, which means that when usinglen
:count
must not be set to anything else but 1 (by default),it is a good practice to specify the expected
type
as well (list
usually).
- Returns:
The matching element, when
count
is 1, list of matching elements otherwise.
Note
As it makes the API convenient, we deliberately shadow the built-in with the
type
parameter.
- static assertexists(path, err=None, evidence=False)¶
Checks whether a path exists.
- Parameters:
path – Path to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertnotexists(path, err=None, evidence=False)¶
Checks whether a path does not exist.
- Parameters:
path – Path to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertisfile(path, err=None, evidence=False)¶
Checks whether a path is a regular file.
- Parameters:
path – Path to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertisdir(path, err=None, evidence=False)¶
Checks whether a path is a directory.
- Parameters:
path – Path to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertsamepaths(path1, path2, err=None, evidence=False)¶
Checks whether two paths are actually the same, even though they may be absolute or relative, or accessed through a symbolic link…
- Parameters:
path1 – First path to check.
path2 – Second path to check.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertisrelativeto(path, dir, err=None, evidence=False)¶
Checks whether a path is a sub-path of a directory.
- Parameters:
path – Path to check.
dir – Container directory candidate.
err – Optional error message.
evidence – Evidence activation (see the dedicated note).
- static assertisnotrelativeto(path, dir, err=None, evidence=False)¶
Checks whether a path is not a sub-path of a directory.
- Parameters:
path – Path to check.
dir – Directory expected not to be a container directory for
path
.err – Optional error message.
evidence – Evidence activation (see the dedicated note).