test_server.py - Tests using the web2py server

These tests start the web2py server then submit requests to it. All the fixtures are auto-imported by pytest from conftest.py.


These are listed in the order prescribed by PEP 8.

Standard library

from textwrap import dedent
import json
from threading import Thread
import datetime
import re
import sys
import time

Third-party imports

import pytest
import six

Local imports

from .utils import web2py_controller_import

Debugging notes

Invoke the debugger.

##import pdb; pdb.set_trace()

Put this in web2py code, then use the web-based debugger.

##from gluon.debug import dbg; dbg.set_trace()


Use for easy manual testing of the server, by setting up a user and class automatically. Comment out the line below to enable it.

@pytest.mark.skip(reason="Only needed for manual testing.")
def test_manual(runestone_db_tools, test_user):

Modify this as desired to create courses, users, etc. for manual testing.

    course_1 = runestone_db_tools.create_course()
    test_user("bob", "bob", course_1)

Pause in the debugger until manual testing is done.

    import pdb


def test_killer(test_assignment, test_client, test_user_1, runestone_db_tools):

This test ensures that we have the routing set up for testing properly. This test will fail if routes.py is set up as follows. routes_onerror = [

(‘runestone/static/404’, ‘/runestone/static/fail.html’), (‘runestone/500’, ‘/runestone/default/reportabug.html’), ]

for testing purposes we don’t want web2py to capture 500 errors.

    with pytest.raises(Exception) as excinfo:
        assert test_client.text == ""
    assert "ticket" in str(excinfo.value) or "INTERNAL" in str(excinfo.value)

Validate the HTML produced by various web2py pages. NOTE – this is the start of a really really long decorator for test_1

    "url, requires_login, expected_string, expected_errors",


FIXME: Flashed messages don’t seem to work. (‘admin/index’, False, ‘You must be registered for a course to access this page’, 1), (‘admin/index’, True, ‘You must be an instructor to access this page’, 1),

        ("admin/doc", True, "Runestone Help and Documentation", 1),


        ("assignments/chooseAssignment", True, "Assignments", 2),
        ("assignments/doAssignment", True, "Bad Assignment ID", 2),
        ("assignments/practice", True, "Practice", 1),
        ("assignments/practiceNotStartedYet", True, "test_course_1", 1),