Hy,
I want to make unit test with web2py.
I follow this page :
https://pythonhosted.org/web2py_utils/test_runner.html.
Is Web2py Test Runner always a good way for unittesting ?
After running execute_tests.py, i have got thie result and an error (logged in report file)
Result :
self.assertEquals(self.nbP+1, len(self.db().select(self.db.person.id)))
AssertionError: 2 != 1
It seems the insert was not performed ?
ERROR: test_ajoutPerson (web2py_utils.test_runner.TestAjoutPerson)
----------------------------------------------------------------------
Traceback (most recent call last):
File "xxxxxx/web2py/applications/app1/tests/manage.py", line 28, in test_ajoutPerson
res = self.env['ajoutPerson']("titi")
File "xxxxxx/web2py/applications/app1/controllers/manage.py", line 24, in ajoutPerson
db.person.insert(name=name)
File "xxxxxx/web2py/gluon/packages/dal/pydal/objects.py", line 712, in insert
ret = self._db._adapter.insert(self, self._listify(fields))
File "xxxxxxweb2py/gluon/packages/dal/pydal/adapters/base.py", line 739, in insert
raise e
OperationalError: database is locked
Thanks for help,
Here is the controller's function i want to test.
******************************************************************************************
controller : manage.py
def formAjoutPerson():
form=SQLFORM(db.person) # person is a table that contains one field named 'name'
if form.validate():
name=form.vars.name
mess=ajoutPerson(name)
response.flash = mess
elif form.errors :
response.flash='error in the form'
return locals()
def ajoutPerson(name):
# the libelle must be unique
rows = db(db.person.name == name).select(db.person.id)
if len(rows) == 0:
db.person.insert(name=name)
mess = 'new person added'
else:
mess = 'Error : name already exists
return mess
******************************************************************************************
Here is the test
class TestAjoutPerson(unittest.TestCase):
def setUp(self):
self.env = new_env(app='app1', controller='manage')
self.db = copy_db(self.env, db_name='db', db_link='sqlite:memory')
self.db.person.insert(name="toto")
self.db.commit()
rows=self.db().select(self.db.person.id)
self.nbP=len(rows)
# Test method
def test_ajoutPerson(self):
res = self.env['ajoutPerson']("titi")
self.db.commit()
self.assertEquals("new person added", res)
#TEST
self.assertEquals(self.nbP+1, len(self.db().select(self.db.person.id)))
suite = unittest.TestSuite() # define a group of unit tests
suite.addTest(unittest.makeSuite(TestAjoutTest)) #add to the suite test class
unittest.TextTestRunner(verbosity=2).run(suite)
**************************************************************************************