Created
May 28, 2011 03:50
-
-
Save gilsondev/996576 to your computer and use it in GitHub Desktop.
Script que prepara o ambiente e executa os testes unitários no projeto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf8 | |
import unittest | |
class ContatosModel(unittest.TestCase): | |
def test_teste(self): | |
contatos = db(db.contatos.id > 0).count() | |
self.assertEquals(contatos, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Runs all tests that exist in the tests directories. | |
Models: | |
appname/tests/models | |
File test: ModuleModel | |
For example: ContactModel | |
Controllers: | |
appname/tests/controllers | |
File test: ModuleController | |
For example: ContactController | |
Execute with: | |
> python web2py_tests_beta.py -S appname | |
28/05/2011 | |
Gilson Filho | |
[email protected] | |
Based on code from Jon Vlachoyiannis | |
[email protected] | |
Font Slice: http://www.web2pyslices.com/main/slices/take_slice/67 | |
""" | |
import unittest | |
import glob | |
import sys | |
import os | |
suite = unittest.TestSuite() | |
from copy import copy | |
from gluon import shell | |
# Ambiente | |
def new_env(app='init', controller=None): | |
""" | |
Prepara o ambiente usando o env do shell | |
do web2py para importar os modelos do sistema, | |
alem do controlador usado nos testes na camada | |
de controle | |
""" | |
WEB2PY = os.path.realpath(os.path.dirname(__file__)) | |
dir = os.path.join(WEB2PY, 'applications', sys.argv[2]) | |
_env = shell.env(sys.argv[2],c=None,import_models=True,dir=dir) | |
if controller: | |
execfile(os.path.join(dir, 'controllers', controller + '.py'), _env) | |
return _env | |
# Copiando banco | |
def copy_db(env, db_name='db', db_link='sqlite:memory:'): | |
""" | |
Copia o banco de dados usado para o desenvolvimento | |
para iniciar os testes. | |
""" | |
from gluon.sql import DAL | |
test_db = DAL(db_link) | |
for tablename in env[db_name].tables: | |
table_copy = [copy(f) for f in env[db_name][tablename]] | |
test_db.define_table(tablename, *table_copy, migrate=True) | |
return test_db | |
def execute_tests(): | |
# get all files with tests | |
test_files = glob.glob('applications/'+sys.argv[2]+'/tests/*/*.py') | |
if not test_files: | |
raise Exception("No files found for app: " + sys.argv[2]) | |
# Bring all unit tests in and their controllers/models/whatever | |
for test_file in test_files: | |
g = copy(globals()) | |
execfile(test_file, g) | |
# Create the appropriate class name based on filename and path | |
# TODO: use regex | |
filename = str.capitalize(test_file.split("/")[-1][:-3]) | |
directory = str.capitalize(test_file.split("/")[-2][:-1]) | |
suite.addTest(unittest.makeSuite(g[filename+directory])) | |
unittest.TextTestRunner(verbosity=2).run(suite) | |
return db | |
if __name__ == "__main__": | |
# Preparando o ambiente | |
env = new_env(sys.argv[2]) | |
db = copy_db(env) | |
# Executando os testes | |
execute_tests() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment