Implemented a unit testing framework for Evennia. Unfortunately it seems it is only usable in latest Django SVN, due to a Django bug; Run "manage.py test-evennia" - if you get errors about SUPPORTS_TRANSACTIONS, you are affected by the bug. Since this is only likely to affect evennia devs at this point I added a few base tests in src/objects/tests.py as a template for those willing to help add unit tests.

This commit is contained in:
Griatch 2010-11-21 19:02:24 +00:00
parent a7899e0119
commit 502ebff1a2
7 changed files with 227 additions and 15 deletions

View file

@ -92,7 +92,7 @@ class CmdPassword(MuxCommand):
oldpass = self.lhslist[0] # this is already stripped by parse()
newpass = self.rhslist[0] # ''
try:
uaccount = caller.user
uaccount = caller.player.user
except AttributeError:
caller.msg("This is only applicable for players.")
return

View file

@ -11,11 +11,18 @@ import os
# Tack on the root evennia directory to the python path.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
#------------------------------------------------------------
# Get Evennia version
#------------------------------------------------------------
try:
VERSION = open("%s%s%s" % (os.pardir, os.sep, 'VERSION')).readline().strip()
except IOError:
VERSION = "Unknown version"
#------------------------------------------------------------
# Check so session file exists in the current dir- if not, create it.
#------------------------------------------------------------
_CREATED_SETTINGS = False
if not os.path.exists('settings.py'):
# If settings.py doesn't already exist, create it and populate it with some
@ -88,7 +95,8 @@ from src.settings_default import *
###################################################
# Evennia components (django apps)
###################################################"""
###################################################
"""
settings_file.write(string)
settings_file.close()
@ -97,6 +105,9 @@ from src.settings_default import *
Welcome to Evennia (version %s)!
We created a fresh settings.py file for you.""" % VERSION
#------------------------------------------------------------
# Test the import of the settings file
#------------------------------------------------------------
try:
from game import settings
except Exception:
@ -114,10 +125,36 @@ except Exception:
print string
sys.exit(1)
# check required versions
#------------------------------------------------------------
# Test runner setup
#------------------------------------------------------------
os.environ['DJANGO_SETTINGS_MODULE'] = 'game.settings'
from django.test.simple import DjangoTestSuiteRunner
class EvenniaTestSuiteRunner(DjangoTestSuiteRunner):
"""
This test runner only runs tests on the apps specified in src/ and game/ to
avoid running the large number of tests defined by Django
"""
def build_suite(self, test_labels, extra_tests=None, **kwargs):
"""
Build a test suite for Evennia. test_labels is a list of apps to test.
If not given, a subset of settings.INSTALLED_APPS will be used.
"""
if not test_labels:
test_labels = [applabel.rsplit('.', 1)[1] for applabel in settings.INSTALLED_APPS
if (applabel.startswith('src.') or applabel.startswith('game.'))]
return super(EvenniaTestSuiteRunner, self).build_suite(test_labels, extra_tests=extra_tests, **kwargs)
def run_suite(self, test_labels=None, extra_tests=None, **kwargs):
"Run wrapper for the tests"
return super(EvenniaTestSuiteRunner, self).run_suite(self.build_suite(test_labels, extra_tests), **kwargs)
#------------------------------------------------------------
# This is run only if the module is called as a program
#------------------------------------------------------------
if __name__ == "__main__":
from django.core.management import execute_manager
# checks if the settings file was created this run
if _CREATED_SETTINGS:
print """
Edit your new settings.py file as needed, then run
@ -125,7 +162,15 @@ if __name__ == "__main__":
create the database and your superuser account.
"""
sys.exit()
# run the django setups
from src.utils.utils import check_evennia_dependencies
# running the unit tests
if len(sys.argv) > 1 and sys.argv[1] == 'test-evennia':
print "Running Evennia-specific test suites ..."
EvenniaTestSuiteRunner(sys.argv[2:]).run_suite()
sys.exit()
# run the standard django manager, if dependencies match
from src.utils.utils import check_evennia_dependencies
if check_evennia_dependencies():
from django.core.management import execute_manager
execute_manager(settings)