Passing parameters to PHPUnit

后端 未结 9 614
我寻月下人不归
我寻月下人不归 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:20

    I don't think answers above solve my same problem.

    The accepted answer is not perfect. In this way, custom options should always be put to the end of the parameters list, and there is no indicator to tell that they are custom options. If the number of custom options which I need is not fixed, I should code a lot to parse custom options with regular expressions or something like that.

    The environment variables solution is good, but not natural. Looks weird.

    VAR1=aaa VAR2=bbb VAR3=ccc ./phpunit-custom-option CustomOptionTest.php
    

    The shell script plus setUp() solution share the same weakness with the accepted one. May be you should code a lot to parse the file and handle unpredictable numbers of custom options.

    I don't think the bootstrap script is the correct solution. It could be used to handle dirty works automatically, with doing same things every time but not dealing with change parts good.

    I don't like all the above answers.

    And I have no good idea myself too. But maybe what I've done could give you inspiration. I've forked the phpunit project on GitHub, and modified code a little, and made it to support the custom option feature.

    enter image description here

    Modified version of phpunit, could accept custom options like this:

    ./phpuint-custom-option --custom var1=value1 --custom var2=value2 CustomOptionTest.php
    

    And in the test, you can visit the custom options by accessing the super global variables $_SERVER

    <?php
    
    class CustomOptionTest extends PHPUnit_Framework_TestCase {
    
        public function testCustomOption() {
            $this->assertEquals('value1', $_SERVER['var1']);
            $this->assertEquals('value2', $_SERVER['var2']);
        }
    }
    

    and you can find my code here, and download the modified version here (by click the "view the full file" link on the page).

    FYI. this article is the similar solution.

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

    Passing arguments on the command line would make sense if you want to vary the test parameters per test run. Running host-specific tests on different machines is not the best justification for this solution.

    For that, the PHPUnit configuration file may prove to be more suitable. It gives you control over host- and even request-specific variables including manipulating php.ini settings as well as defining constants, global variables, $_ENV, $_SERVER, and even $_GET, $_POST, etc. This is all done under the <php> node of the configuration file, see Setting PHP INI settings, Constants and Global Variables

    Symfony2 uses this approach and provides both a phpunit.xml.dist (default config) and a phpunit.xml with your unit tests. The latter is gitignored to allow you to customize it for each machine without affecting the repo. You would then run your tests with:

    phpunit -c /path/to/phpunit.xml
    
    0 讨论(0)
  • 2020-12-24 11:26

    All the solutions here are valid for the question, but there is yet another way that might be simpler for some situations. Phing will take arguments passed in the form -Dargument=value

    So using phing -Dtest=MyTest.class.php

    You can then use phing conditionals to handle these arguments:

    <if>
        <isset property="test" />
        <then>
            <property name="testFile" value="${test}" />
        </then>
        <else>
            <property name="testFile" value="AllTests.php" />
        </else>
    </if>
    <exec command="phpunit --bootstrap myTestFile/bootstrap.php- myTestFolder/${testFile}"
          passthru="true" returnproperty="phpunitreturn" />
    
    0 讨论(0)
  • 2020-12-24 11:27

    You can use PHPUnit's --bootstrap switch for this.

    --bootstrap <file>       A "bootstrap" PHP file that is run before the tests.
    

    Then, make a bootstrap.php file that contains variables:

    $port = 4445;
    

    In your tests, you can grab those values:

    global $port;
    $this->setPort($port);
    

    Then run:

    phpunit --bootstrap boot.php MyTest.php
    
    0 讨论(0)
  • 2020-12-24 11:31

    As Jasir already mentioned, a one line solution would be to set environment variable before phpunit call.

    On Linux:

    X=alpha phpunit unittest.php
    

    On Windows probably:

    set X=johns_laptop && phpunit.bat unittest.php
    

    And inside your script use

    getenv('X')
    

    to read the value

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

    One way would be for you to inspect $argv and $argc. Something like:

    <?php
    
    require_once 'PHPUnit/Framework/TestCase.php';
    
    class EnvironmentTest extends PHPUnit_Framework_TestCase {
        public function testHasParam() {
                global $argv, $argc;
                $this->assertGreaterThan(2, $argc, 'No environment name passed');
                $environment = $argv[2];
        }
    }
    

    Then you can call your phpunittest like this:

    phpunit EnvironmentTest.php my-computer
    
    0 讨论(0)
提交回复
热议问题