12

I have a nose test that uses a pathname to a png file in the tests directory. One path works in local testing, one path works on Travis. How do I check when the code is run on Travis?

Edit: Here is the actual code.

ArekBulski
  • 4,520
  • 4
  • 39
  • 61

3 Answers3

18

To check the existence of TRAVIS:

import os
is_travis = 'TRAVIS' in os.environ
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
6

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'
Nakilon
  • 34,866
  • 14
  • 107
  • 142
rkersh
  • 4,447
  • 2
  • 22
  • 31
  • 1
    The documentation says that `TRAVIS=true` is the default. Depending on how paranoid you want to be, you can check that the value is actually "true". I included the example to show that. Using `os.getenv` with a default of 'false' may be better here. – rkersh Aug 22 '16 at 22:09
  • Also consider looking at `CI` and `CONTINUOUS_INTEGRATION` variables, mentioned in the same doc. – Nakilon Nov 08 '19 at 23:57
1

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
RayLuo
  • 17,257
  • 6
  • 88
  • 73
  • 1
    Yep hours. Obviously, just cating the env isn't good enough, if you do it before calling tox... – mdurant Sep 22 '20 at 16:42