开发者

How to set specific class properties for a PHPunit test?

What is a good best practice for phpunit to set some default configuration values in a test?

I'm thinking of values like

  • username
  • client id no.
  • so on..

    Normally I would set this using the constructor or some setters but I'm开发者_JS百科 not sure how to do this when I only have a class like this

    class RestTest extends PHPUnit_Framework_TestCase {
    

    For example I'm trying to make test cases for a REST and SOAP service.

    I need to specify the url/user and pass but they change on dev/test/production so I need a clean way to specify them.


    If you have some default values like a UserId that you need in every testcase of your test I'd suggest setting those in the setUp method of your class.

    Just an example for setting the values:

    class MtTest extends PHPUnit_Framework_TestCase {
    
        // As a property
        private $testUser = 7;
    
        // In the setUp
        public function setUp() {
            $this->testUser = 7;
            $this->defaultName = "BOB";
            $this->MtClass = new Mt();
        }
    
        public function testFoo() {
             $this->assertTrue($this->MtClass->userExists($this->testUser));
        }
    }
    

    If you have a set of values that you need to feed into one test method use DataProviders like @SilverLight (+1) suggested.

    Response to comment:

    If you need some kind of environment setup (for whatever you are trying to do, I'll just go with username for now since i can't guess) you could set those in your PHPUnit Bootstrap (or the file you pass to --bootstrap):

    bootstrap.php

    <?php
    define("PHPUnit_Username", "Fuuu");
    // or
    $phpunitConfig = new Array(
         "username" => "fuu",
    )
    // or whatever suits you there. No reason for me to type a config object i guess
    class MyPHPUnitConfig {
         public function getUsername() { return "wtf"; }
    }
    

    and maybe have your own base Testclass

    <?php
    class MyBaseTest extends PHPUnit_Framework_TestCase {
    
        public function setUp() {
            parent::setUp();
            $this->config = new MyPHPUnitConfig();
        }
    }
    

    and extend from that. Or use setUpBeforeClass to only do it once per test class.


    If that doesn't answer your question i just don't get what you are ameing at. It's a testcase, it doesn't have any strong dependencies and it kinda shouldn't have any (Isolation) and i don't guess your problem is how to get a value into your setUp function is it? (Because thats kinda not a phpunit issue).

    Let me know if that helped


    I think you are looking for data providers:

    <?php
    class DataTest extends PHPUnit_Framework_TestCase
    {
        /**
         * @dataProvider provider
         */
        public function testAdd($a, $b, $c)
        {
            $this->assertEquals($c, $a + $b);
        }
    
        public function provider()
        {
            return array(
              array(0, 0, 0),
              array(0, 1, 1),
              array(1, 0, 1),
              array(1, 1, 3)
            );
        }
    }
    ?>
    

    data provider can contain any type of information, including usersname, client id etc.

  • 0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜