Test parameters and naming
Test Parameters
Test parameters can be used to set attributes of a test. Here is a list of most common parameters for a test:
✋ Test parameters are used to set attributes of a test. Not to be confused with the attributes which is just one of the attributes of the test (object), that can be specified using the
attributesparameter when calling or creating a test.
- name
- flags
- uid
- tags
- attributes
- requirements
- examples
- description
- xargs
- xfails
- xflags
- ffails
- repeats
- retries
- timeouts
- only
- skip
- start
- end
- only_tags
- skip_tags
- random
- limit
- args
✋ Most parameter names match the names of the attributes of the test which they set. For example, name parameter sets the
nameattribute of the test.
✋ Note that the Args decorator can be used to set values of any parameter of the test. However, many parameters have corresponding dedicated decorators available to be used instead.
When test is defined inline then parameters can be set right when a test definition class is instantiated.
The first parameter is always name which sets the name of the test. The other parameters are usually
specified using keyword arguments.
For example,
1 | with Scenario("My test", description="This is a description of an inline test"): |
Test Arguments
args
The args parameter is used to set the arguments of the test.
✋ Do not confuse the
argsparameter with the Args decorator. See the Args decorator for more details.
1 |
|
If test arguments are passed during the test call, then they will overwrite the args.
For example,
1 |
|
Args
The Args class can be used as a decorator to set any parameters of the test. This is especially useful when there is no dedicated decorator available for the parameter.
Each test parameter can be specified using a corresponding keyword argument.
1 |
For example, the Name decorator can be used to set the name parameter of the test, however, the same can be done using the Args decorator.
1 |
|
It can also be used to specify default values of test arguments by setting the args parameter
to a dictionary where the key is the argument name and the value is the argument’s value.
1 |
|
Naming Tests
You can set the name of any test either by setting the name parameter of the inline test
or using the Name decorator if the test is defined as a decorated function.
The name of the test can be accessed using the name attribute of the test.
Restricted Characters
✋ New behaviour starting with version >=
2.3.11
TestFlows, by default, allows any character to be used in test names. However, some characters
are considered restricted. Specifically, the following ASCII characters /"'$\[]*?:!.^+{}|()
are restricted and, in general, are not recommended to be used in
test names as they cause conflicts when used in bash, regex, or name patterns.
If the test program is launched with the --strict-names option, then the NameError exception is raised
for any test whose name contains one or more restricted character.
When test program is executed without the --strict-names option, TestFlows will
automatically convert restricted characters to their UTF-8 replacements according to the following table:
| Restricted Character | Replacement Code | Replacement Rendered | Replacement Description | Replacement Reason |
|---|---|---|---|---|
/ |
U+2215 | ∕ |
DIVISION SLASH | path separator |
" |
U+FF02 | " |
FULLWIDTH QUOTATION MARK | bash |
' |
U+FF07 | ' |
FULLWIDTH APOSTROPHE | bash |
$ |
U+FE69 | ﹩ |
SMALL DOLLAR SIGN | bash / regex |
\ |
U+FE68 | ﹨ |
SMALL REVERSE SOLIDUS | bash |
[ |
U+FF3B | [ |
FULLWIDTH LEFT SQUARE BRACKET | pattern / regex |
] |
U+FF3D | ] |
FULLWIDTH RIGHT SQUARE BRACKET | pattern / regex |
* |
U+FF0A | * |
FULLWIDTH ASTERISK | pattern / regex |
? |
U+FE16 | ︖ |
PRESENTATION FORM FOR VERTICAL QUESTION MARK | pattern / regex |
: |
U+FE55 | ﹕ |
SMALL COLON | pattern |
! |
U+FE15 | ︕ |
PRESENTATION FORM FOR VERTICAL EXCLAMATION MARK | bash |
. |
U+2024 | ․ |
ONE DOT LEADER | regex |
^ |
U+02C4 | ˄ |
MODIFIED LETTER UP ARROWHEAD | regex |
+ |
U+FF0B | + |
FULLWIDTH PLUS SIGN | regex |
{ |
U+FF5B | { |
FULLWIDTH LEFT CURLY BRACKET | regex |
} |
U+FF5D | } |
FULLWIDTH RIGHT CURLY BRACKET | regex |
| |
U+2160 | Ⅰ |
ROMAN NUMERAL ONE | regex |
( |
U+FF08 | ( |
FULLWIDTH LEFT PARENTHESIS | regex |
) |
U+FF09 | ) |
FULLWIDTH RIGHT PARENTHESIS | regex |
For example,
1 | from testflows.core import * |
1 | $ python3 test.py |
The test name "/"'$\[]*?:!.^+{}|()" will be converted to ∕"'﹩﹨[]*︖﹕︕․˄+{}Ⅰ(.
Even though the original test name contained characters that were conflicting with pattern
special symbols, because of the replacements, you can easily copy-paste the name for the --only option without
worrying about any special character escaping and just adding the /* at the end.
1 | $ python3 test.py --only '/∕"'﹩﹨[]*︖﹕︕․˄+{}Ⅰ()/*' |
Executing the same test program with the --strict-names option will result in an error.
1 | $ python3 test.py --strict-names |
Using clean()
The clean() function, from the testflows.core.name module, can be used to convert restricted characters to their UTF-8 replacements.
This is needed, for example, when using test names that contain restricted characters in xfails.
1 | from testflows.core import * |
You can also skip the explicit testflows.core.name import and use the default name.clean reference.
1 | from testflows.core import * |
Using strict mode
Use the --strict-names test program option to force the strict names mode which disallows using any restricted characters
in the test names. Any test whose name contains one or more restricted characters will cause a NameError exception to be raised.
1 | from testflows.core import * |
1 | $ python3 test.py --strict-names |
name
The name parameter of the test can be use used to set the name of any inline test. The name parameter
must be passed a str which will define the name of the test.
✋ For all test definition classes the first parameter is always the name.
For example,
1 | with Test("My test") as test: |
Name
A Name decorator can be used to set the name of any test that is defined using a decorated function.
✋ The name of test defined using a decorated function is set to the name of the function if the Name decorator is not used.
For example,
1 |
|
or if the Name decorator is not used
✋ Note that any underscores will be replaced with spaces in the name of the test.
1 |
|