Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

    like image 594
    johnlemon Avatar asked Apr 11 '26 02:04

    johnlemon


    1 Answers

    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

    like image 78
    edorian Avatar answered Apr 13 '26 14:04

    edorian



    Donate For Us

    If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!