35

I need a way to determine if the person calling the function is Travis-CI or not. If it is Travis-CI, I do not want to start a session here. Right now, I have my tests script create a file called test.txt and then look for it.

protected function __construct() {
    if ( (!session_id()) && (!file_exists('test.txt' ))) session_start();
}

However, there has to be a better way. It seems, that without the file check, if there is in fact a session made, a new one will not be created. But this is not the case. If this was the case, the before link below should have passed.

Before the addition of the "test.txt" file:
After

chriscct7
  • 527
  • 5
  • 11
  • 3
    Systems that explicitly change their behaviour when being tested are **not** being tested properly. – paxdiablo Oct 19 '12 at 22:14
  • 3
    @paxdiablo Not really. The issue at hand is that to test my wp plugin, we use a dependency which starts the session. Unfortunately, I can't turn that off. – chriscct7 Oct 19 '12 at 22:17

3 Answers3

58

In general you can detect if you are on Travis-CI by checking the environment variables. You can check either for CI=true or the more specific TRAVIS=true. In PHP you can use the getenv() function to get the value of an environment variable.

See the complete list of the environment. You can set even more env variables in your .travis.yml.

Edric
  • 24,639
  • 13
  • 81
  • 91
Odi
  • 6,916
  • 3
  • 34
  • 52
1

If you're using JavaScript, there is a great module called is-ci which is available in NPM and is MIT Licensed.

Note: it gives true for any Continuous Integration environment, not only TravisCI.

Pedro A
  • 3,989
  • 3
  • 32
  • 56
0

For Java i am checking the user:

/**
 * check if we are in the Travis-CI environment
 * @return true if Travis user was detected
 */
public boolean isTravis() {
  String user = System.getProperty("user.name");
  return user.equals("travis");
} 
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186