开发者

Could you post examples on how to use PHPUnit with NetBeans?

开发者 https://www.devze.com 2023-04-09 16:38 出处:网络
I\'m new to PHPUnit and I want to use it with Netbeans. I\'m already aware that there is documentation for PHPUnit on it\'s own but there isn\'t much documentation on how to use it with Netbeans. It w

I'm new to PHPUnit and I want to use it with Netbeans. I'm already aware that there is documentation for PHPUnit on it's own but there isn't much documentation on how to use it with Netbeans. It would be great to see some working examples first. I learn better that way.

So from the Netbeans site they give this example and then you would Right Click File->Create PHPUnit Tests to automatically generate PHPUnit classes:

class Calculator{
    /**
     * @assert (0,0) == 0
     * @assert (0,1) == 1
     * @assert (1,0) == 1
     * @assert (1,1) == 2
     * @assert (1,2) == 4
     */
    public function add($a, $b){
        return $a + $b;
    }

}

However, im reading another PHP book and in it they do it this way

class Session {
    public function __construct($one, $two){}
    public function login(){}
    public function isLoggedIn() {return null;}
}

require_once("PHPUnit/Autoload.php");

class TestSession extends PHPUnit_Framework_TestCase {

    private $_session;

    function setUp() {
        $dsn = array(
            'phptype' => "pgsql",
            'hostspec' => "localhost",
            'database' => "widgetworld",
            'username' => "wuser",
         开发者_开发百科   'password' => "foobar"
        );
        $this->_session = new Session($dsn, true);
    }

    function testValidLogin() {
        $this->_session->login("ed", "12345");
        $this->assertEquals(true, $this->_session->isLoggedIn());
    }

    function testInvalidLogin() {
        $this->_session->login("ed", "54321"); // fail
        $this->assertEquals(false, $this->_session->isLoggedIn());
    }
}
$suite = new PHPUnit_Framework_TestSuite;
$suite->addTest(new TestSession("testValidLogin"));
$suite->addTest(new TestSession("testInvalidLogin"));
$testRunner = new PHPUnit_TextUI_TestRunner();
$testRunner->run( $suite );

Could you help me understand how to do PHPUnit tests in Netbeans by converting the above example? Thanks

I don't know but would doing something like this in Netbeans be correct?:

class Session {

    public function __construct($one, $two) {}

    public function login() {
        /**
     * @assert ("ed", "12345")->isLoggedIn() == true
     */

     /**
     * @assert ("ed", "54321")->isLoggedIn() == false
     */
    }

    public function isLoggedIn() {return null;}

}  


I have never use @assert in NetBeans, but it looks like they simply inform the test case generator how to write the tests. In the end, NetBeans creates a test case for the class which will look similar to TestSession above (though it would be called SessionTest).

If you use @assert to define your test data, keep in mind that you'll need to regenerate the test every time you change them, and this will overwrite any changes you made to the test case after doing so. I would recommend writing the test cases by hand as you can't use @assert as you write it to test the session. It is designed to write tests like "Given parameters X, Y, and Z, the return value should be R." You can't use it to test side effects of your methods.


To do PHPUnit testing in Netbeans, do the following things.

  1. Go to "File" > "Project properties" from the main men
  2. Then select click on "PHPUnit" tab on vertical menu on the left of the dialog window
  3. Choose the option how you will run PHPUnit such as using bootstrap file or XML or custom suites
  4. Then, go to "Run" > "Test Project" from the main menu
  5. Then, you will prompted for selecting where are the test files
  6. Then, you will also prompted for where is your PHPUnit shell script or bat file(on Windows) on your computer if you haven't configured it for Netbeans

Once you setup properly like above, every time you need to run test cases, going to "Run" > "Test Project" from the menu

0

精彩评论

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

关注公众号