Testing¶
Assertion Utils¶
-
class
CommonAssertionUtils
¶ Bases:
object
Utilities for used to assert within tests. They can be used outside tests if need be.
-
static
are_equal
(value_one, value_two, message='')¶ Assert two values are equal to each other.
If the values are both collections, then the values contained within will be asserted to be equal.
Note
The order of the values in each collection is asserted.
Parameters: - value_one (Any) – The first value.
- value_two (Any) – The second value.
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True, if the assertion succeeds.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
are_similar
(value_one, value_two, message='')¶ Assert two values are similar.
If the values are both collections, then the values contained within will be asserted to be similar.
Note
The order of the values in each collection is ignored.
Parameters: - value_one (Any) – The first value.
- value_two (Any) – The second value.
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True, if the assertion succeeds.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
contains
(collection, value, message='')¶ Assert a value is contained within a collection
Parameters: - collection (Union[Tuple[Any], List[Any], Dict[Any, Any]]) – The collection being checked (Any collection that works with len() can be used)
- value (Any) – The value being located.
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True, if the value is contained within the collection. False, if it is not.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
has_length
(value, expected_length, message='')¶ Assert a collection has the specified length.
Parameters: - value (Union[Tuple[Any], List[Any]]) – The collection being asserted. (Any collection that works with len() can be used)
- expected_length (int) – The length expected of the collection.
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True if the length matches.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
is_false
(value, message='')¶ Assert value is False.
Parameters: - value (Union[bool, CommonTestResult, CommonExecutionResult]) – The value being asserted.
- message (str, optional) – A custom message to include when the assertion fails.
Returns: True if the value is False.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
is_true
(value, message='')¶ Assert a value is True.
Parameters: - value (Union[bool, CommonTestResult, CommonExecutionResult]) – The value being asserted.
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True if the value is True.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
list_contents_are_same
(list_one, list_two, message='')¶ Assert the values contained within two collections are the same.
Note
The order of the values in each collection is ignored.
Parameters: - list_one (Union[Tuple[Any], List[Any]]) – The first value. (Can be any collection type)
- list_two (Union[Tuple[Any], List[Any]]) – The second value. (Can be any collection type)
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True, if the assertion succeeds.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
lists_are_equal
(list_one, list_two, message='')¶ Assert two collections contain tbe exact same values.
Note
The order of the values in each collection will be asserted.
Parameters: - list_one (Union[Tuple[Any], List[Any]]) – The first value. (Can be any collection type)
- list_two (Union[Tuple[Any], List[Any]]) – The second value. (Can be any collection type)
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True, if the assertion succeeds.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
not_contains
(collection, value, message='')¶ Assert a value is NOT contained within a collection
Parameters: - collection (Union[Tuple[Any], List[Any], Dict[Any, Any]]) – The collection being checked (Any collection that works with len() can be used)
- value (Any) – The value being located.
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True, if the value is NOT contained within the collection. False, if it is.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
not_throws
(callback, message='')¶ Assert calling a function will not raise an Exception.
Parameters: - callback (Callable[.., Any]) – The function to invoke.
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: True, if the assertion was successful.
Return type: bool
Raises: AssertionError – when the assertion fails.
-
static
throws
(callback, message='')¶ Assert calling a function will raise an Exception.
Parameters: - callback (Callable[.., Any]) – The function to invoke.
- message (str, optional) – A custom message to include when the assertion fails. Default is Empty String.
Returns: The exception that was thrown.
Return type: Exception
Raises: AssertionError – when the assertion fails.
-
static
Test Service¶
-
class
CommonTestService
¶ Bases:
sims4communitylib.services.common_service.CommonService
Use to register and run python tests.
Example Test Registration: @CommonTestService.test_class('mod_name') class TestClass: # Important that it is a static method, you won't get a cls or self value passed in, so don't expect one! @staticmethod @CommonTestService.test(1, 1, expected_value=2) @CommonTestService.test(2, 1, expected_value=3) def example_add_test(one: int, two: int, expected_value: int=24): result = one + two CommonAssertionUtils.are_equal(result, expected_value, message='{} plus {} did not equal {}!'.format(one, two, expected_value))
-
get_test_library_by_mod
(mod_identifier)¶ Retrieve a library of tests for a Mod organized by class name.
Parameters: - mod_identifier – The name or identity of the mod to search tests for.
- mod_identifier – Union[str, CommonModIdentity]
Returns: A library of tests for the specified Mod organized by class name.
Return type: Dict[str, Any]
-
get_tests_by_mod_name
(mod_name)¶ Retrieve tests by a mod name.
Parameters: - mod_identifier – The name or identity of the mod that owns the tests within the class.
- mod_identifier – Union[str, CommonModIdentity]
Returns: A collection of tests for the specified Mod.
Return type: Tuple[Any]
-
run_tests
(class_names=tuple(), callback=print)¶ Run all tests for the specified classes names.
Parameters: - class_names (Tuple[str]) – A collection of classes to run tests for.
- callback (Callable[str, Any]) – Any time a message needs to be printed or logged, it will be sent to this callback. Default is print.
-
run_tests_by_mod_name
(mod_identifier, class_names=tuple(), callback=print)¶ Run all tests for the specified classes names for a specific Mod.
Parameters: - mod_identifier – The name or identity of the mod to run tests for.
- mod_identifier – Union[str, CommonModIdentity]
- class_names (Tuple[str], optional) – A collection of classes to run tests for. Default is all tests.
- callback (Callable[str, Any]) – Any time a message needs to be printed or logged, it will be sent to this callback. Default is print.
-
static
test
(args, kwargs)¶ Decorate a function with this to register that function as a test. When the test is run, it will be sent the specified arguments and keyword arguments.
Warning
The function being registered MUST be a staticmethod.
Parameters: - args (Any) – The arguments that will be sent to the function upon run.
- kwargs (Any) – The keyword arguments that will be sent to the function upon run.
Returns: A function wrapped to run a test upon invocation.
Return type: Callable[.., Any]
-
static
test_class
(mod_identity)¶ Decorate a class with this to register a class as containing tests.
Parameters: - mod_identity – The identity of the mod that owns the tests within the class.
- mod_identity – CommonModIdentity
Returns: A class wrapped to run tests.
Return type: Any
-