Detecting when code is run on Travis CI
You could check for the existence (or value) of an environment variable. It looks like Travis defines several by default (see here).
For example:
import os
istravis = os.environ.get('TRAVIS') == 'true'
In hindsight, all the answers above were correct. However, I would also like to document another cause that wasted me hours of my life.
If you happen to be maintaining a code base which uses the popular tox to orchestrate your tests, you might not know this tox behavior:
By default tox will only pass the PATH environment variable (and on windows SYSTEMROOT and PATHEXT) from the tox invocation to the test environments. If you want to pass down additional environment variables you can use the passenv option:
[testenv]
passenv = TRAVIS
To check the existence of TRAVIS:
import os
is_travis = 'TRAVIS' in os.environ