Skip to main content

nose-knows is a nose plugin for figuring out which unit tests you should run after modifying code. It works by tracing your code while you run unit tests, and creating an output file that can be used later.

Installing

You can install nose-knows through pip or easy-install:

pip install nose-knows

Or you can download the latest development version, which may contain new features.

Using nose-knows

nose-knows can be invoked in either input or output mode. In output mode (--knows-out) it will generate a .knows file that contains the mapping from a file in your code tree to the set of unit tests that run it. In input mode, it uses the .knows file to selectively run unit tests based on the files you pass in. Note: here, $BASE_DIR is the name of the base project directory, not the (direct / relative) path to it. It is used to figure out how to rename the file names in the output file to make it more portable.

Creating a .knows file:

eyal-01575:src eyal$ nosetests --with-knows --knows-dir=$BASE_DIR --knows-out
.....................................................................
----------------------------------------------------------------------
Ran 62 tests in 0.444s

OK

The .knows file now contains the following:

warehouse/src/load_data/sql_utils/checkpoints.py:
    src.tests.test_load_data_statements:TestSQLStatement.test_finalize
    src.tests.test_load_data_checkpoints:TestCheckpoints.test_checkpoint_finalize_with_delete
    src.tests.test_load_data_checkpoints:TestCheckpoints.test_checkpoint_finalize
    src.tests.test_load_data_checkpoints:TestCheckpoints.test_checkpoint_with_previous_checkin
    src.tests.test_load_data_checkpoints:TestCheckpoints.test_checkpoint
    src.tests.test_load_data_statements:TestSQLStatement.test_to_sql_import
    src.tests.test_load_data_statements:TestSQLStatement.test_to_sql_schema_update

You can now run nose-knows in input mode, passing in load_data/sql_utils/checkpoints.py:

eyal-01575:src eyal$ nosetests --with-knows --knows-dir=$BASE_DIR load_data/sql_utils/checkpoints.py
.......
----------------------------------------------------------------------
Ran 7 tests in 0.003s

OK

There is also (experimental at this point) support for py.test. You can generate your .knows file via:

eyal-01575:src eyal$ py.test --with-knows --knows-dir=$BASE_DIR --knows-out
===================== test session starts ======================
platform darwin -- Python 2.7.1 -- pytest-2.3.4
plugins: nose-knows
collected 62 items

tests/test_load_data_checkpoints.py ....
tests/test_load_data_from_mysql.py ..
tests/test_load_data_statements.py .....
tests/test_process_data_denormalize.py ........
tests/test_process_data_mapping.py .....
tests/test_transform_data_daemon.py .
tests/test_transform_data_table_follower.py .................
tests/test_transformers/test_avg_data.py ..
tests/test_transformers/test_count.py ...
tests/test_transformers/test_join.py ....
tests/test_transformers/test_json_data.py .....
tests/test_transformers/test_min_data.py ..
tests/test_transformers/test_std_data.py ..
tests/test_transformers/test_sum_data.py ..

================== 62 passed in 2.18 seconds ===================

And selectively run specific unit tests like so:

eyal-01575:src eyal$ py.test --with-knows --knows-dir=$BASE_DIR load_data/sql_utils/checkpoints.py
===================== test session starts ======================
platform darwin -- Python 2.7.1 -- pytest-2.3.4
plugins: nose-knows
collected 62 items

tests/test_load_data_checkpoints.py ....
tests/test_load_data_statements.py ...

=================== 7 passed in 0.30 seconds ===================

The best practice here is to have a system like Jenkins run the unit test suite once in a while to create this map (we have it running daily), and then creating a bash function/script to download the knows output file from Jenkins and run it against the set of changed files from a commit. Ours looks like:

function grab_latest_knows_output() {
    NOW=`date +%s`
    if [ ! -f $KNOWS_FILE_TMP ] ; then
        curl --compressed $KNOWS_FILE_URL > $KNOWS_FILE_TMP
    else
        KNOWS_FILE_AGE=`stat -c %Y $KNOWS_FILE_TMP`
        if [ `expr $NOW - $KNOWS_FILE_AGE` -gt "86400" ] ; then
            curl --compressed $KNOWS_FILE_URL > $KNOWS_FILE_TMP
        else
            echo "Using latest knows output file."
        fi
    fi
}

function test_changed() {
    grab_latest_knows_output
    nosetests $KNOWS_FLAGS `git diff --name-only --cached origin | xargs`
}

function run_tests_for() {
    grab_latest_knows_output
    nosetests $KNOWS_FLAGS $@
}

License

nose-knows is copyright 2013 Eventbrite and Contributors, and is made available under BSD-style license; see LICENSE for details.

News

0.1.2

Adding support for non-unittest based tests with nose.

0.1.1

Minor bug fix to make the nose plugin more resilient to bad test names.

0.1

Release date: April 2nd, 2012

  • Initial write of nose-knows, including support for nose and experimental support for py.test

Metadata

Release files for nose-knows 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nose-knows 0.2
File Size Uploaded Provenance
nose-knows-0.2.tar.gz 7.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for nose-knows 0.2
File Interpreter ABI Platform
nose_knows-0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 16.7 kB

Release files / nose-knows-0.2.tar.gz

Download URL nose-knows-0.2.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3ac979adb044ffc8b0a33a357c34dc255ed91141857be5c1755906880b8a1b75
BLAKE2b-256 checksum
How to use checksums
4499d2dfbd0ec485069b54cb420fd1f1e57c918818d675600000171cf505fdd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / nose_knows-0.2-py2.py3-none-any.whl

Download URL nose_knows-0.2-py2.py3-none-any.whl
Size 9.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
caaaa2138d0d7e9c1a17f19a7479dd9507749a69d91aaeec83569c09386d9cea
BLAKE2b-256 checksum
How to use checksums
e67250246fec1c2db153fc67760eed0749d0ffc7ebb91065e611acc3c016c631
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for nose-knows 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page