/web2py.test

A reasonable way to implement and run test cases in a Web2py application, using py.test

Primary LanguagePythonOtherNOASSERTION

Web2py.test

An example of how to test an Web2py Application.

I use py.test [1] to test this application, but the concept can be applied in unittest and nose, too.

Tested in Web2py v2.7.4 and v2.9.5

IMPORTANT: I recommend you work with virtualenv to give you more freedom. It's not required, but strongly recommended.

Stepping into

The procedure to run tests present for this application is:

  1. Create a new virtualenv. Let's call it bla:

     $ cd ~
     $ mkdir -p virtualenvs/bla
     $ cd virtualenvs/bla
     $ virtualenv .
    
  2. Enter into it:

     $ source bin/activate
    
  3. Now your prompt should look like this:

     (bla)username@yourmachine:~$
    
  4. Install py.test just in your virtualenv:

     $ pip install pytest
    
  5. Download latest web2py stable:

     $ wget http://www.web2py.com/examples/static/web2py_src.zip
    
  6. Unzip it:

     $ unzip web2py_src.zip
    
  7. Now you must see web2py dir created inside your current dir.

  8. Enter into it:

     $ cd web2py
    
  9. Get the latest web2py.test:

     $ git clone https://github.com/viniciusban/web2py.test.git applications/people
    
  10. You must see the people dir inside your applications directory.

  11. Start Web2py development server:

     $ python web2py.py -a my_password --nogui &
    
  12. Run tests:

     $ py.test -x -v -s applications/people/tests
    

VoilĂ !

Understanding

To understand the method used to allow running tests, refer to web2py/applications/people/tests/conftest.py

Read web2py/applications/people/models/db.py to see how to make your application know she is running under the test environment.

Test cases are in web2py/applications/people/tests subdirs.

There are 3 important parts in this application:

  1. tests/conftest.py -> configure test environment.
  2. modules/web2pytest/web2pytest.py -> test infrastructure.
  3. models/db.py -> ask web2pytest about tests and create db accordingly.

Links used in this doc: