Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Ensure the test suite is executed against the local tornado modules. #545

wants to merge 5 commits into from

3 participants


In case tornado is installed globally on the system, i.e in site-packages, it will compromise our execution of tornado/test/ Because the global version of tornado will be imported instead of the local one in which the executed script resides.

This pull request resolves this issue by injecting the local tornado directory path to our sys.path prior to importing any tornado modules in the script. The path is inserted at index 1 since the current directory should remain the primary one.

@birknilson birknilson Ensure the test suite is executed against the local tornado modules.
This is achieved by injecting the path of the tornado module in which
the test suite is located in our sys.path.

This is why I prefer the python -m style of running python programs. It guarantees a consistent set of modules, and in a little-known twist adds the current directory to the PYTHONPATH. I'm reluctant to add explicit sys.path manipulation in code; it's come back to bite me too often.


I agree the best solution is to execute the script with the -m flag. However, I do think we could add a simple bash script which invokes the script with the -m flag. Which could reside under tornado/test/run for instance and be the recommended endpoint to execute the test suite - which would be specified by the documentation instead of the current

I execute the test suite too often without -m since the script itself has the necessary shebang and permissions to execute it directly without invoking python -m. It's too easy to trick oneself into believing everything works :)


+1 for the dedicated bash script, but should be called runtests and live in the toplevel directory of the repository.


I have now changed this pull request to utilize a newly introduced bash script instead of the prior sys.path manipulation. The bash script is named and placed according to @alekstorm's suggestion and it merely executes the script, but as a module using the python -m invokation.

This solution will not break any existing scripts people might have constructed to automatically execute the script & it will work in the same way as before. However, the runtests bash script will ensure local modules are imported and is therefore the recommended script to utilize during development & testing. The relevant docstrings in the testing module has been changed to reflect this and promote the new bash alternative.


LGTM, but I meant the toplevel directory of the repository (tornado contains source files and gets copied to the user's site-packages directory). As in, make this script a sibling of tornado. That way, the entirety of the script can be:

#!/usr/bin/env bash
python -m tornado.test.runtests $*

How do other projects do this?

@bdarnell bdarnell closed this pull request from a commit
@bdarnell bdarnell Encourage "python -m tornado.test.runtests" as the way to run the tests.
Remove execute bit from tornado/test/, which would unexpectedly
run the tests from a globally-installed version of tornado when run directly.
Add a new to make the recommended behavior more discoverable.
Remove, which has been superceded by tox.
Update docs.

Closes #545.
@bdarnell bdarnell closed this in ef9df4d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 20, 2012
  1. @birknilson

    Ensure the test suite is executed against the local tornado modules.

    birknilson authored
    This is achieved by injecting the path of the tornado module in which
    the test suite is located in our sys.path.
Commits on Jun 21, 2012
  1. @birknilson
  2. @birknilson

    Added bash script which will execute the entire test suite.

    birknilson authored
    This script will ensure to execute the test suite against the local
    tornado modules in which the script itself resides rather than any
    system wide installation.
  3. @birknilson
  4. @birknilson
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 2 deletions.
  1. +19 −0 tornado/runtests
  2. +2 −2 tornado/
19 tornado/runtests
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+# Execute the entire tornado test suite and ensure the local tornado
+# modules are imported rather than the system wide packages.
+# Retrieve the absolute path of the root tornado directory
+# in which this script is located.
+while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
+TORNADO_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )/../" && pwd )"
+# Change directory to the root tornado directory
+# Execute the entire test suite script as a module.
+# All arguments passed to this script will be passed along to the
+# tornado.test.runtests module.
+python -m tornado.test.runtests $*
4 tornado/
@@ -388,9 +388,9 @@ def main():
be overridden by naming a single test on the command line::
# Runs all tests
- tornado/test/
+ tornado/runtests
# Runs one test
- tornado/test/ tornado.test.stack_context_test
+ tornado/runtests tornado.test.stack_context_test
from tornado.options import define, options, parse_command_line
Something went wrong with that request. Please try again.