Passing parameters to PHPUnit

后端 未结 9 615
我寻月下人不归
我寻月下人不归 2020-12-24 11:03

I\'m starting to write PHPUnit tests and I\'d like the tests to be run from developers machines as well as from our servers. Developers machines are set up differently than

相关标签:
9条回答
  • 2020-12-24 11:35

    An elegant way to pass variables to both bootstrap files as well as to test files is by using environment variables:

    export MY_ENV_VAR="some value"
    
    phpunit all
    

    Then, in your PHP files, you can access it like this:

    getenv('MY_ENV_VAR')
    

    Source: http://blog.lysender.com/2010/10/phpunit-passing-environment-variable-to-your-application/

    0 讨论(0)
  • 2020-12-24 11:40

    If you would like to run tests on remote machine, use ssh then run it. On locale machine you only have to cd to your root dir, then run phpunit.

    user@local:/path/to/your/project$ phpunit
    user@remote:/var/www/project$ phpunit
    

    Edit: You are talking about a machine dependent configuration. (What kind of conf btw?) My solution is to put these config under the same, not versioncontrolled place, then read/parse it runtime, in the needed set up methds for example.

    0 讨论(0)
  • 2020-12-24 11:43

    I struggled with this exact issue, and came up with a kind of hacky-yet-convenient solution: I write parameters to a file on disk and retrieve them in the setUp() method:

    public function setUp() {
        $this->setBrowser('firefox');
        $this->base_url = file_get_contents("selenium_host");
        $this->setBrowserUrl($this->base_url);
    }
    

    Rather than calling phpunit or paratest directly, I have a shell script to launch the tests. This one invokes paratest and lets me specify the number of processes as well as the host I'd like the tests to run against.

    run_all_tests.sh

    if [ $1 ] 
    then
        threads=$1
    else
        threads=5
    fi
    if [ $2 ]
    then
        echo $2 > selenium_host
    else
        echo 'http://defaulthost.com' > selenium_host
    fi
    
    vendor/bin/paratest -p$threads -f --colors TestSuite.php
    

    Then, to run with 7 threads against http://adifferenthost.com:

    ./run_all_tests.sh 7 'http://adifferenthost.com'

    0 讨论(0)
提交回复
热议问题