<h1>Unit Testing<aclass="headerlink"href="#unit-testing"title="Permalink to this headline">¶</a></h1>
<p><em>Unit testing</em> means testing components of a program in isolation from each other to make sure every part works on its own before using it with others. Extensive testing helps avoid new updates causing unexpected side effects as well as alleviates general code rot (a more comprehensive wikipedia article on unit testing can be found <aclass="reference external"href="http://en.wikipedia.org/wiki/Unit_test">here</a>).</p>
<p>A typical unit test set calls some function or method with a given input, looks at the result and makes sure that this result looks as expected. Rather than having lots of stand-alone test programs, Evennia makes use of a central <em>test runner</em>. This is a program that gathers all available tests all over the Evennia source code (called <em>test suites</em>) and runs them all in one go. Errors and tracebacks are reported.</p>
<p>By default Evennia only tests itself. But you can also add your own tests to your game code and have Evennia run those for you.</p>
<p>This will run all the evennia tests using the default settings. You could also run only a subset of all tests by specifying a subpackage of the library:</p>
<p>A temporary database will be instantiated to manage the tests. If everything works out you will see how many tests were run and how long it took. If something went wrong you will get error messages. If you contribute to Evennia, this is a useful sanity check to see you haven’t introduced an unexpected bug.</p>
<h2>Running tests with custom settings file<aclass="headerlink"href="#running-tests-with-custom-settings-file"title="Permalink to this headline">¶</a></h2>
<p>If you have implemented your own tests for your game (see below) you can run them from your game dir with</p>
<p>The period (<codeclass="docutils literal notranslate"><spanclass="pre">.</span></code>) means to run all tests found in the current directory and all subdirectories. You could also specify, say, <codeclass="docutils literal notranslate"><spanclass="pre">typeclasses</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">world</span></code> if you wanted to just run tests in those subdirs.</p>
<p>Those tests will all be run using the default settings. To run the tests with your own settings file you must use the <codeclass="docutils literal notranslate"><spanclass="pre">--settings</span></code> option:</p>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">--settings</span></code> option of Evennia takes a file name in the <codeclass="docutils literal notranslate"><spanclass="pre">mygame/server/conf</span></code> folder. It is normally used to swap settings files for testing and development. In combination with <codeclass="docutils literal notranslate"><spanclass="pre">test</span></code>, it forces Evennia to use this settings file over the default one.</p>
</div>
<divclass="section"id="writing-new-tests">
<h2>Writing new tests<aclass="headerlink"href="#writing-new-tests"title="Permalink to this headline">¶</a></h2>
<p>Evennia’s test suite makes use of Django unit test system, which in turn relies on Python’s <em>unittest</em> module.</p>
<blockquote>
<div><p>If you want to help out writing unittests for Evennia, take a look at Evennia’s <aclass="reference external"href="https://coveralls.io/github/evennia/evennia">coveralls.io page</a>. There you see which modules have any form of test coverage and which does not.</p>
</div></blockquote>
<p>To make the test runner find the tests, they must be put in a module named <codeclass="docutils literal notranslate"><spanclass="pre">test*.py</span></code> (so <codeclass="docutils literal notranslate"><spanclass="pre">test.py</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">tests.py</span></code> etc). Such a test module will be found wherever it is in the package. It can be a good idea to look at some of Evennia’s <codeclass="docutils literal notranslate"><spanclass="pre">tests.py</span></code> modules to see how they look.</p>
<p>Inside a testing file, a <codeclass="docutils literal notranslate"><spanclass="pre">unittest.TestCase</span></code> class is used to test a single aspect or component in various ways. Each test case contains one or more <em>test methods</em> - these define the actual tests to run. You can name the test methods anything you want as long as the name starts with “<codeclass="docutils literal notranslate"><spanclass="pre">test_</span></code>”. Your <codeclass="docutils literal notranslate"><spanclass="pre">TestCase</span></code> class can also have a method <codeclass="docutils literal notranslate"><spanclass="pre">setUp()</span></code>. This is run before each test, setting up and storing whatever preparations the test methods need. Conversely, a <codeclass="docutils literal notranslate"><spanclass="pre">tearDown()</span></code> method can optionally do cleanup after each test.</p>
<p>To test the results, you use special methods of the <codeclass="docutils literal notranslate"><spanclass="pre">TestCase</span></code> class. Many of those start with “<codeclass="docutils literal notranslate"><spanclass="pre">assert</span></code>”, such as <codeclass="docutils literal notranslate"><spanclass="pre">assertEqual</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">assertTrue</span></code>.</p>
<p>Example of a <codeclass="docutils literal notranslate"><spanclass="pre">TestCase</span></code> class:</p>
<p>You might also want to read the <aclass="reference external"href="http://docs.python.org/library/unittest.html">documentation for the unittest module</a>.</p>
<h3>Using the EvenniaTest class<aclass="headerlink"href="#using-the-evenniatest-class"title="Permalink to this headline">¶</a></h3>
<p>Evennia offers a custom TestCase, the <codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.test_resources.EvenniaTest</span></code> class. This class initiates a range of useful properties on themselves for testing Evennia systems. Examples are <codeclass="docutils literal notranslate"><spanclass="pre">.account</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">.session</span></code> representing a mock connected Account and its Session and <codeclass="docutils literal notranslate"><spanclass="pre">.char1</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">char2</span></code> representing Characters complete with a location in the test database. These are all useful when testing Evennia system requiring any of the default Evennia typeclasses as inputs. See the full definition of the <codeclass="docutils literal notranslate"><spanclass="pre">EvenniaTest</span></code> class in <aclass="reference external"href="https://github.com/evennia/evennia/blob/master/evennia/utils/test_resources.py">evennia/utils/test_resources.py</a>.</p>
<h3>Testing in-game Commands<aclass="headerlink"href="#testing-in-game-commands"title="Permalink to this headline">¶</a></h3>
<p>In-game Commands are a special case. Tests for the default commands are put in <codeclass="docutils literal notranslate"><spanclass="pre">evennia/commands/default/tests.py</span></code>. This uses a custom <codeclass="docutils literal notranslate"><spanclass="pre">CommandTest</span></code> class that inherits from <codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.test_resources.EvenniaTest</span></code> described above. <codeclass="docutils literal notranslate"><spanclass="pre">CommandTest</span></code> supplies extra convenience functions for executing commands and check that their return values (calls of <codeclass="docutils literal notranslate"><spanclass="pre">msg()</span></code> returns expected values. It uses Characters and Sessions generated on the <codeclass="docutils literal notranslate"><spanclass="pre">EvenniaTest</span></code> class to call each class).</p>
<p>Each command tested should have its own <codeclass="docutils literal notranslate"><spanclass="pre">TestCase</span></code> class. Inherit this class from the <codeclass="docutils literal notranslate"><spanclass="pre">CommandTest</span></code> class in the same module to get access to the command-specific utilities mentioned.</p>
<h3>Unit testing contribs with custom models<aclass="headerlink"href="#unit-testing-contribs-with-custom-models"title="Permalink to this headline">¶</a></h3>
<p>A special case is if you were to create a contribution to go to the <codeclass="docutils literal notranslate"><spanclass="pre">evennia/contrib</span></code> folder that uses its <aclass="reference internal"href="New-Models.html"><spanclass="doc">own database models</span></a>. The problem with this is that Evennia (and Django) will only recognize models in <codeclass="docutils literal notranslate"><spanclass="pre">settings.INSTALLED_APPS</span></code>. If a user wants to use your contrib, they will be required to add your models to their settings file. But since contribs are optional you cannot add the model to Evennia’s central <codeclass="docutils literal notranslate"><spanclass="pre">settings_default.py</span></code> file - this would always create your optional models regardless of if the user wants them. But at the same time a contribution is a part of the Evennia distribution and its unit tests should be run with all other Evennia tests using <codeclass="docutils literal notranslate"><spanclass="pre">evennia</span><spanclass="pre">test</span><spanclass="pre">evennia</span></code>.</p>
<p>The way to do this is to only temporarily add your models to the <codeclass="docutils literal notranslate"><spanclass="pre">INSTALLED_APPS</span></code> directory when the test runs. here is an example of how to do it.</p>
<blockquote>
<div><p>Note that this solution, derived from this <aclass="reference external"href="http://stackoverflow.com/questions/502916/django-how-to-create-a-model-dynamically-just-for-testing#503435">stackexchange answer</a> is currently untested! Please report your findings.</p>
<h3>A note on adding new tests<aclass="headerlink"href="#a-note-on-adding-new-tests"title="Permalink to this headline">¶</a></h3>
<p>Having an extensive tests suite is very important for avoiding code degradation as Evennia is developed. Only a small fraction of the Evennia codebase is covered by test suites at this point. Writing new tests is not hard, it’s more a matter of finding the time to do so. So adding new tests is really an area where everyone can contribute, also with only limited Python skills.</p>
<h3>A note on making the test runner faster<aclass="headerlink"href="#a-note-on-making-the-test-runner-faster"title="Permalink to this headline">¶</a></h3>
<p>If you have custom models with a large number of migrations, creating the test database can take a very long time. If you don’t require migrations to run for your tests, you can disable them with the django-test-without-migrations package. To install it, simply:</p>
<p>Then add it to your <codeclass="docutils literal notranslate"><spanclass="pre">INSTALLED_APPS</span></code> in your <codeclass="docutils literal notranslate"><spanclass="pre">server.conf.settings.py</span></code>:</p>
<p>After doing so, you can then run tests without migrations by adding the <codeclass="docutils literal notranslate"><spanclass="pre">--nomigrations</span></code> argument:</p>
<h2>Testing for Game development (mini-tutorial)<aclass="headerlink"href="#testing-for-game-development-mini-tutorial"title="Permalink to this headline">¶</a></h2>
<p>Unit testing can be of paramount importance to game developers. When starting with a new game, it is recommended to look into unit testing as soon as possible; an already huge game is much harder to write tests for. The benefits of testing a game aren’t different from the ones regarding library testing. For example it is easy to introduce bugs that affect previously working code. Testing is there to ensure your project behaves the way it should and continue to do so.</p>
<p>If you have never used unit testing (with Python or another language), you might want to check the <aclass="reference external"href="https://docs.python.org/2/library/unittest.html">official Python documentation about unit testing</a>, particularly the first section dedicated to a basic example.</p>
<h3>Basic testing using Evennia<aclass="headerlink"href="#basic-testing-using-evennia"title="Permalink to this headline">¶</a></h3>
<p>Evennia’s test runner can be used to launch tests in your game directory (let’s call it ‘mygame’). Evennia’s test runner does a few useful things beyond the normal Python unittest module:</p>
<ulclass="simple">
<li><p>It creates and sets up an empty database, with some useful objects (accounts, characters and rooms, among others).</p></li>
<li><p>It provides simple ways to test commands, which can be somewhat tricky at times, if not tested properly.</p></li>
</ul>
<p>Therefore, you should use the command-line to execute the test runner, while specifying your own game directories (not the one containing evennia). Go to your game directory (referred as ‘mygame’ in this section) and execute the test runner:</p>
<p>This command will execute Evennia’s test runner using your own settings file. It will set up a dummy database of your choice and look into the ‘commands’ package defined in your game directory (<codeclass="docutils literal notranslate"><spanclass="pre">mygame/commands</span></code> in this example) to find tests. The test module’s name should begin with ‘test’ and contain one or more <codeclass="docutils literal notranslate"><spanclass="pre">TestCase</span></code>. A full example can be found below.</p>
</div>
<divclass="section"id="a-simple-example">
<h3>A simple example<aclass="headerlink"href="#a-simple-example"title="Permalink to this headline">¶</a></h3>
<p>In your game directory, go to <codeclass="docutils literal notranslate"><spanclass="pre">commands</span></code> and create a new file <codeclass="docutils literal notranslate"><spanclass="pre">tests.py</span></code> inside (it could be named anything starting with <codeclass="docutils literal notranslate"><spanclass="pre">test</span></code>). We will start by making a test that has nothing to do with Commands, just to show how unit testing works:</p>
<p>This example, inspired from the Python documentation, is used to test the ‘upper()’ method of the ‘str’ class. Not very useful, but it should give you a basic idea of how tests are used.</p>
<p>Let’s execute that test to see if it works.</p>
<spanclass="p">(</span><spanclass="n">Obs</span><spanclass="p">:</span><spanclass="n">Evennia</span><spanclass="s1">'s full test suite may not pass if the settings are very</span>
<p>We specified the <codeclass="docutils literal notranslate"><spanclass="pre">commands</span></code> package to the evennia test command since that’s where we put our test file. In this case we could just as well just said <codeclass="docutils literal notranslate"><spanclass="pre">.</span></code> to search all of <codeclass="docutils literal notranslate"><spanclass="pre">mygame</span></code> for testing files. If we have a lot of tests it may be useful to test only a single set at a time though. We get an information text telling us we are using our custom settings file (instead of Evennia’s default file) and then the test runs. The test passes! Change the “FOO” string to something else in the test to see how it looks when it fails.</p>
</div>
<divclass="section"id="testing-commands">
<h3>Testing commands<aclass="headerlink"href="#testing-commands"title="Permalink to this headline">¶</a></h3>
<p>This section will test the proper execution of the ‘abilities’ command, as described in the <aclass="reference internal"href="First-Steps-Coding.html"><spanclass="doc">First Steps Coding</span></a> page. Follow this tutorial to create the ‘abilities’ command, we will need it to test it.</p>
<p>Testing commands in Evennia is a bit more complex than the simple testing example we have seen. Luckily, Evennia supplies a special test class to do just that … we just need to inherit from it and use it properly. This class is called ‘CommandTest’ and is defined in the ‘evennia.commands.default.tests’ package. To create a test for our ‘abilities’ command, we just need to create a class that inherits from ‘CommandTest’ and add methods.</p>
<p>We could create a new test file for this but for now we just append to the <codeclass="docutils literal notranslate"><spanclass="pre">tests.py</span></code> file we already have in <codeclass="docutils literal notranslate"><spanclass="pre">commands</span></code> from before.</p>
<li><p>Line 1-4: we do some importing. ‘CommandTest’ is going to be our base class for our test, so we need it. We also import our command (‘CmdAbilities’ in this case). Finally we import the ‘Character’ typeclass. We need it, since ‘CommandTest’ doesn’t use ‘Character’, but ‘DefaultCharacter’, which means the character calling the command won’t have the abilities we have written in the ‘Character’ typeclass.</p></li>
<li><p>Line 6-8: that’s the body of our test. Here, a single command is tested in an entire class. Default commands are usually grouped by category in a single class. There is no rule, as long as you know where you put your tests. Note that we set the ‘character_typeclass’ class attribute to Character. As explained above, if you didn’t do that, the system would create a ‘DefaultCharacter’ object, not a ‘Character’. You can try to remove line 4 and 8 to see what happens when running the test.</p></li>
<li><p>Line 10-11: our unique testing method. Note its name: it should begin by ‘test_’. Apart from that, the method is quite simple: it’s an instance method (so it takes the ‘self’ argument) but no other arguments are needed. Line 11 uses the ‘call’ method, which is defined in ‘CommandTest’. It’s a useful method that compares a command against an expected result. It would be like comparing two strings with ‘assertEqual’, but the ‘call’ method does more things, including testing the command in a realistic way (calling its hooks in the right order, so you don’t have to worry about that).</p></li>
</ul>
<p>Line 11 can be understood as: test the ‘abilities’ command (first parameter), with no argument (second parameter), and check that the character using it receives his/her abilities (third parameter).</p>
<p>Two tests were executed, since we have kept ‘TestString’ from last time. In case of failure, you will get much more information to help you fix the bug.</p>