Although web2py is recommended to use doctest to test the controller in the document, I am not familiar with doctest, and I only perform test coverage on the Controller layer. Although it is comprehensive, the test granularity is too large, the measurement is not detailed enough for me.
Me
The design habit is that the controller layer only performs simple call conversion, and the actual business logic is placed in the module layer (Note: not the model layer) for processing, although it is set in the Model
Generic
View can also do a lot of work, but on the one hand I am not used to this method, on the other hand, this method still has some limitations-the most important thing is that I don't want to put too many items on the interface into the model.
Both
However, the business logic is in the module, so I need to test these modules separately without using the page, this is what the recommended test method in this document cannot do.
Try to get a few pages. So I studied how to use unittest to test the module. The key to the problem is how to create a web2py runtime environment in unittest.
Environment. The core of this environment is DB-how to initialize a DB for the tested module.
In fact, it is not difficult to achieve this. The gluon. shell package provides the exec_environment function to implement this function. Assume that there is a DB. py as follows:
DB. define_table ('tabletest', <br/> sqlfield ('code'), <br/> sqlfield ('name '))
The test case can be written as follows:
Import sys <br/> Import OS <br/> db_path = path. realpath ('.. /models/DB. PY ') <br/> web2py_path = '.. /.. /.. '<br/> sys. path. append (OS. path. realpath (web2py_path) <br/> sys. path. append (OS. path. realpath ('.. ') <br/> OS. chdir (web2py_path) <br/> Import unittest <br/> from gluon. shell import exec_environment <br/> from modules import datamodule <br/> class testdefacontroller controller (unittest. testcase): <br/> def setup (Self): <br/> self. DBM = exec_environment (db_path) <br/> def teardown (Self): <br/> DB = self. dBm. DB <br/> query = (dB. tabletest. code = "000111") <br/> dB (query ). delete () <br/> def testdb (Self): <br/> DB = self. dBm. DB <br/> X = dB. tabletest. insert (code = "000111", name = "test name") <br/> query = (dB. tabletest. id = x) <br/> Y = dB (query ). select () [0]. ID <br/> self. assertequal (x, y) <br/> def testdatamodule (Self): <br/> DB = self. dBm. DB <br/> X = datamodule. foo (dB ,...) <br/> self. assert... <br/> If _ name _ = '_ main _': <br/> unittest. main ()
The code is very simple. The first section is the path for preparing the program, because the test case file is stored in: web2py/applications/yourapp/tests, so ".. /.. /.. "to point to the root directory of web2py and locate the current location here.
Heavy
The point is that the setup function calls the exec_environment function to execute dB. py, so that you can prepare the running environment of web2py for the test and enable dB. py.
The model definition in. Then, you can use the database in testdb and testdatamodule to access the database and check the execution results. This is similar to General unittest applications.
There is no difference.
Note that web2py of Windows EXE cannot be used. You must use web2py of source code to use unittest normally. Otherwise, an error occurs.