SQLALCHEMY UNIT TESTS
SQLite support is required. These instructions assume standard Python 2.4 or
The 'test' directory must be on the PYTHONPATH.
cd into the SQLAlchemy distribution directory
$ export PYTHONPATH=./test/
C:\sa\> set PYTHONPATH=test\
Adjust any other use Unix-style paths in this README as needed.
The unittest framework will automatically prepend the lib/ directory to
sys.path. This forces the local version of SQLAlchemy to be used, bypassing
any setuptools-installed installations (setuptools places .egg files ahead of
plain directories, even if on PYTHONPATH, unfortunately).
RUNNING ALL TESTS
To run all tests:
$ python test/alltests.py
RUNNING INDIVIDUAL TESTS
Any unittest module can be run directly from the module file:
To run a specific test within the module, specify it as ClassName.methodname:
python test/orm/mapper.py MapperTest.testget
COMMAND LINE OPTIONS
Help is available via --help
$ python test/alltests.py --help
usage: alltests.py [options] [tests...]
-h, --help show this help message and exit
--verbose enable stdout echoing/printing
--quiet suppress output
Command line options can applied to alltests.py or any individual test module.
Many are available. The most commonly used are '--db' and '--dburi'.
Tests will target an in-memory SQLite database by default. To test against
another database, use the --dburi option with any standard SQLAlchemy URL:
Use an empty database and a database user with general DBA privileges. The
test suite will be creating and dropping many tables and other DDL, and
preexisting tables will interfere with the tests
If you'll be running the tests frequently, database aliases can save a lot of
typing. The --dbs option lists the built-in aliases and their matching URLs:
$ python test/alltests.py --dbs
Available --db options (use --dburi to override)
To run tests against an aliased database:
$ python test/alltests.py --db=postgres
To customize the URLs with your own users or hostnames, make a simple .ini
file called `test.cfg` at the top level of the SQLAlchemy source distribution
or a `.satest.cfg` in your home directory:
Your custom entries will override the defaults and you'll see them reflected
in the output of --dbs.
SQLAlchemy logs its activity and debugging through Python's logging package.
Any log target can be directed to the console with command line options, such
$ python test/orm/unitofwork.py --log-info=sqlalchemy.orm.mapper \
This would log mapper configuration, connection pool checkouts, and SQL
BUILT-IN COVERAGE REPORTING
Coverage is tracked with coverage.py module, included in the './test/'
directory. Running the test suite with the --coverage switch will generate a
local file ".coverage" containing coverage details, and a report will be
printed to standard output with an overview of the coverage gathered from the
last unittest run (the file is deleted between runs).
After the suite has been run with --coverage, an annotated version of any
source file can be generated, marking statements that are executed with > and
statements that are missed with !, by running the coverage.py utility with the
"-a" (annotate) option, such as:
$ python ./test/testlib/coverage.py -a ./lib/sqlalchemy/sql.py
This will create a new annotated file ./lib/sqlalchemy/sql.py,cover. Pretty
BIG COVERAGE TIP !!! There is an issue where existing .pyc files may
store the incorrect filepaths, which will break the coverage system. If
coverage numbers are coming out as low/zero, try deleting all .pyc files.
TESTING NEW DIALECTS
You can use the SQLAlchemy test suite to test any new database dialect in
development. All possible database features will be exercised by default.
Test decorators are provided that can exclude unsupported tests for a
particular dialect. You'll see them all over the source, feel free to add
your dialect to them or apply new decorations to existing tests as required.
It's fine to start out with very broad exclusions, e.g. "2-phase commit is not
supported on this database" and later refine that as needed "2-phase commit is
not available until server version 8".
To be considered for inclusion in the SQLAlchemy distribution, a dialect must
be integrated with the standard test suite. Dialect-specific tests can be
placed in the 'dialects/' directory. Comprehensive testing of
database-specific column types and their proper reflection are a very good
place to start.
When working through the tests, start with 'engine' and 'sql' tests. 'engine'
performs a wide range of transaction tests that might deadlock on a brand-new
dialect- try disabling those if you're having problems and revisit them later.
Once the 'sql' tests are passing, the 'orm' tests should pass as well, modulo
any adjustments needed for SQL features the ORM uses that might not be
available in your database. But if an 'orm' test requires changes to your
dialect or the SQLAlchemy core to pass, there's a test missing in 'sql'! Any
time you can spend boiling down the problem to it's essential sql roots and
adding a 'sql' test will be much appreciated.
The test suite is very effective at illuminating bugs and inconsistencies in
an underlying DB-API (or database!) implementation. Workarounds are almost
always possible. If you hit a wall, join us on the mailing list or, better,
Postgres: The tests require an 'alt_schema' and 'alt_schema_2' to be present in
the testing database.
Postgres: When running the tests on postgres, postgres can get slower and
slower each time you run the tests. This seems to be related to the constant
creation/dropping of tables. Running a "VACUUM FULL" on the database will
speed it up again.
MSSQL: Tests that involve multiple connections require Snapshot Isolation
ability implented on the test database in order to prevent deadlocks that will
occur with record locking isolation. This feature is only available with
MSSQL 2005 and greater. For example::
ALTER DATABASE MyDatabase
SET ALLOW_SNAPSHOT_ISOLATION ON
ALTER DATABASE MyDatabase
SET READ_COMMITTED_SNAPSHOT ON