evennia.utils.test_resources

Various helper resources for writing unittests.

Classes for testing Evennia core:

  • BaseEvenniaTestCase - no default objects, only enforced default settings

  • BaseEvenniaTest - all default objects, enforced default settings

  • BaseEvenniaCommandTest - for testing Commands, enforced default settings

Classes for testing game folder content:

  • EvenniaTestCase - no default objects, using gamedir settings (identical to

    standard Python TestCase)

  • EvenniaTest - all default objects, using gamedir settings

  • EvenniaCommandTest - for testing game folder commands, using gamedir settings

Other:

  • EvenniaTestMixin - A class mixin for creating the test environment objects, for making custom tests.

  • EvenniaCommandMixin - A class mixin that adds support for command testing with the .call() helper. Used by the command-test classes, but can be used for making a customt test class.

evennia.utils.test_resources.mockdelay(timedelay, callback, *args, **kwargs)[source]
evennia.utils.test_resources.mockdeferLater(reactor, timedelay, callback, *args, **kwargs)[source]
evennia.utils.test_resources.unload_module(module)[source]

Reset import so one can mock global constants.

Parameters

module (module, object or str) – The module will be removed so it will have to be imported again. If given an object, the module in which that object sits will be unloaded. A string should directly give the module pathname to unload.

Example

# (in a test method)
unload_module(foo)
with mock.patch("foo.GLOBALTHING", "mockval"):
    import foo
    ... # test code using foo.GLOBALTHING, now set to 'mockval'

This allows for mocking constants global to the module, since otherwise those would not be mocked (since a module is only loaded once).

class evennia.utils.test_resources.EvenniaTestMixin[source]

Bases: object

Evennia test environment mixin

account_typeclass

alias of evennia.accounts.accounts.DefaultAccount

object_typeclass

alias of evennia.objects.objects.DefaultObject

character_typeclass

alias of evennia.objects.objects.DefaultCharacter

exit_typeclass

alias of evennia.objects.objects.DefaultExit

room_typeclass

alias of evennia.objects.objects.DefaultRoom

script_typeclass

alias of evennia.scripts.scripts.DefaultScript

create_accounts()[source]
teardown_accounts()[source]
create_rooms()[source]
create_objs()[source]
create_chars()[source]
create_script()[source]
setup_session()[source]
teardown_session()[source]
setUp()[source]

Sets up testing environment

tearDown()[source]
class evennia.utils.test_resources.EvenniaCommandTestMixin[source]

Bases: object

Mixin to add to a test in order to provide the .call helper for testing the execution and returns of a command.

Tests a Command by running it and comparing what messages it sends with expected values. This tests without actually spinning up the cmdhandler for every test, which is more controlled.

Example:

from commands.echo import CmdEcho

class MyCommandTest(EvenniaTest, CommandTestMixin):

    def test_echo(self):
        '''
        Test that the echo command really returns
        what you pass into it.
        '''
        self.call(MyCommand(), "hello world!",
                  "You hear your echo: 'Hello world!'")
call(cmdobj, input_args, msg=None, cmdset=None, noansi=True, caller=None, receiver=None, cmdstring=None, obj=None, inputs=None, raw_string=None, use_assertequal=False)[source]

Test a command by assigning all the needed properties to a cmdobj and running the sequence. The resulting .msg calls will be mocked and the text= calls to them compared to a expected output.

Parameters
  • cmdobj (Command) – The command object to use.

  • input_args (str) – This should be the full input the Command should see, such as ‘look here’. This will become .args for the Command instance to parse.

  • msg (str or dict, optional) – This is the expected return value(s) returned through caller.msg(text=…) calls in the command. If a string, the receiver is controlled with the receiver kwarg (defaults to caller). If this is a dict, it is a mapping {receiver1: “expected1”, receiver2: “expected2”,…} and receiver is ignored. The message(s) are compared with the actual messages returned to the receiver(s) as the Command runs. Each check uses .startswith, so you can choose to only include the first part of the returned message if that’s enough to verify a correct result. EvMenu decorations (like borders) are stripped and should not be included. This should also not include color tags unless noansi=False. If the command returns texts in multiple separate .msg- calls to a receiver, separate these with | if noansi=True (default) and || if noansi=False. If no msg is given (None), then no automatic comparison will be done.

  • cmdset (str, optional) – If given, make .cmdset available on the Command instance as it runs. While .cmdset is normally available on the Command instance by default, this is usually only used by commands that explicitly operates/displays cmdsets, like examine.

  • noansi (str, optional) – By default the color tags of the msg is ignored, this makes them significant. If unset, msg must contain the same color tags as the actual return message.

  • caller (Object or Account, optional) – By default self.char1 is used as the command-caller (the .caller property on the Command). This allows to execute with another caller, most commonly an Account.

  • receiver (Object or Account, optional) – This is the object to receive the return messages we want to test. By default this is the same as caller (which in turn defaults to is self.char1). Note that if msg is a dict, this is ignored since the receiver is already specified there.

  • cmdstring (str, optional) – Normally this is the Command’s key. This allows for tweaking the .cmdname property of the Command**. This isb used for commands with multiple aliases, where the command explicitly checs which alias was used to determine its functionality.

  • obj (str, optional) – This sets the .obj property of the Command - the object on which the Command ‘sits’. By default this is the same as caller. This can be used for testing on-object Command interactions.

  • inputs (list, optional) – A list of strings to pass to functions that pause to take input from the user (normally using @interactive and ret = yield(question) or evmenu.get_input). Each element of the list will be passed into the command as if the user answered each prompt in that order.

  • raw_string (str, optional) – Normally the .raw_string property is set as a combination of your key/cmdname and input_args. This allows direct control of what this is, for example for testing edge cases or malformed inputs.

  • use_assertequal (bool, optional) – If True, the error message will use a regular assertEqual. This will show show whitepace differences easier, but doesn’t allow for only matching against the start of the returned message.

Returns

str or dict

The message sent to receiver, or a dict of

{receiver: “msg”, …} if multiple are given. This is usually only used with msg=None to do the validation externally.

Raises

AssertionError – If the returns of .msg calls (tested with .startswith) does not match expected_input.

Notes

As part of the tests, all methods of the Command will be called in the proper order:

  • cmdobj.at_pre_cmd()

  • cmdobj.parse()

  • cmdobj.func()

  • cmdobj.at_post_cmd()

class evennia.utils.test_resources.BaseEvenniaTestCase(methodName='runTest')[source]

Bases: django.test.testcases.TestCase

Base test (with no default objects) but with enforced default settings.

tearDown() → None[source]

Hook method for deconstructing the test fixture after testing it.

class evennia.utils.test_resources.EvenniaTestCase(methodName='runTest')[source]

Bases: django.test.testcases.TestCase

For use with gamedir settings; Just like the normal test case, only for naming consistency.

Notes:

  • Inheriting from this class will bypass EvenniaTestMixin, and therefore not setup some default objects. This can result in faster tests.

  • If you do inherit from this class for your unit tests, and have overridden the tearDown() method, please also call flush_cache(). Not doing so will result in flakey and order-dependent tests due to the Django ID cache not being flushed.

tearDown() → None[source]

Hook method for deconstructing the test fixture after testing it.

class evennia.utils.test_resources.BaseEvenniaTest(methodName='runTest')[source]

Bases: evennia.utils.test_resources.EvenniaTestMixin, django.test.testcases.TestCase

This class parent has all default objects and uses only default settings.

class evennia.utils.test_resources.EvenniaTest(methodName='runTest')[source]

Bases: evennia.utils.test_resources.EvenniaTestMixin, django.test.testcases.TestCase

This test class is intended for inheriting in mygame tests. It helps ensure your tests are run with your own objects and settings from your game folder.

account_typeclass = 'typeclasses.accounts.Account'
object_typeclass = 'typeclasses.objects.Object'
character_typeclass = 'typeclasses.characters.Character'
exit_typeclass = 'typeclasses.exits.Exit'
room_typeclass = 'typeclasses.rooms.Room'
script_typeclass = 'typeclasses.scripts.Script'
class evennia.utils.test_resources.BaseEvenniaCommandTest(methodName='runTest')[source]

Bases: evennia.utils.test_resources.BaseEvenniaTest, evennia.utils.test_resources.EvenniaCommandTestMixin

Commands only using the default settings.

class evennia.utils.test_resources.EvenniaCommandTest(methodName='runTest')[source]

Bases: evennia.utils.test_resources.EvenniaTest, evennia.utils.test_resources.EvenniaCommandTestMixin

Parent class to inherit from - makes tests use your own classes and settings in mygame.