Could you give examples of using PHPUnit with NetBeans?

I am new to PHPUnit and I want to use it with Netbeans. I already know that there is documentation for PHPUnit, but documentation on how to use it with Netbeans is scarce. It would be great to see some working examples. I studied so well.

So, from the Netbeans website they give this example, and then you should Right Click File Create PHPUnit Tests 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, I am reading another PHP book, and in it they do it like this.

 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 something like this in Netbeans be right ?:

 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;} } 
+4
source share
2 answers

I have never used @assert in NetBeans, but it looks like they are just informing the test case generator how to write tests. In the end, NetBeans creates a test case for a class that will look like the TestSession above (although it will be called SessionTest ).

If you use @assert to determine your test data, keep in mind that you will need to update the test every time you change it, and after that it will replace all the changes you made to the test case. I would recommend recording test cases manually, since you cannot use @assert when you write it to test a session. It is designed to write tests like "Given the parameters X, Y, and Z, the return value must be equal to R." You cannot use it to check for side effects of your methods.

+3
source

To test PHPUnit with Netbeans, follow these steps:

  • Go to File> Project Properties for Basic Men
  • Then select the "PHPUnit" tab in the vertical menu to the left of the dialog box.
  • Choose how you will run PHPUnit, for example, using a boot file or XML or custom packages
  • Then go to "Run"> "Test Project" from the main menu.
  • Then you will be asked to choose where the test files are
  • Then you also ask where your PHPUnit script shell or bat file (on Windows) is located on your computer if you have not configured it for Netbeans

Once you’ve correctly configured, as described above, each time you need to run test cases, choose Run> Test Project from the menu

+1
source

All Articles