PHP Unit Test Tool phpunit in-depth usage (ii) 1th/2 page _php Tutorial

Source: Internet
Author: User
1, marktestskipped and Marktestincomplete

In PHPUnit, there are two useful ways to marktestskipped and Marktestincomplete. They allow you to write unit tests that do not only pass and fail two results. Marktestskipped allows PHPUnit to not execute a test method that has been written. For example, the following program:

PHP
Public functiontestthismighthaveadb ()
{
$myObject -CreateObject ();
Try {
$db = NewDatabase ();
$this -Asserttrue ($db -rowexists ());
} Catch(Databseexception$e) {
$this -marktestskipped ('This test is skipped because there was a database problem');
}
}
?>

In the above program, it is a test method to determine whether the data exists after connecting to the database, but if you consider the connection exception of the database, you should use marktestskipped to indicate that the test method should be ignored when throwing an exception, because an exception occurs, and when you pay attention, At this point it is possible that the code you write is correct, except that there is an exception, so that the output of phpunit is not simply output fail.

And Marktestincomplete is a bit similar, but a bit different is that it is used when developers write an unfinished test method, marking out that a test method has not been written, the same test results will not be fail, Just tell PHPUnit that this test method has not been written yet, examples are as follows:

PHP
Public functiontestarenotenoughhours ()
{
$this -Marktestincomplete ("there aren ' t enough hours in the day to have my tests go green");
$trueVariable = true;
$this -Asserttrue ($trueVariable);
}
?>

  2. More in-depth understanding of assertions in PHPUnit

In the previous article, we have basically explained the use of assertions in some basic phpunit, here in an example, the following is a class code:

PHP
classtestable
{
Public $trueProperty = true;
Public $resetMe = true;
Public $testArray = Array(
'First key' = 1,
'Second Key' = 2
);
Private $testString = "I do love me some strings";
Public function__construct ()
{
}
Public functionAddvalues ($valueOne,$valueTwo) {
return $valueOne+$valueTwo;
}
Public functiongetteststring ()
{
return $this -teststring;
}
}
?>

The initial framework for the unit test code we write is as follows:

PHP
classtestabletestextendsPhpunit_framework_testcase
{
Private $_testable = NULL;
Public functionsetUp ()
{
$this -_testable= Newtestable ();
}
Public functionTearDown ()
{
$this -_testable= NULL;
}
/** Test methods'll go here*/
}
?>

In the previous article, the Setup method and the Teardown method were introduced, and in the Setup method, the testable () instance was established and stored in the variable $_testable, and the object was destroyed in the Teardown method.

Next, start writing some assertions to test, first look at Asserttrue and assertfalase:

PHP
Public functionTesttruepropertyistrue ()
{
$this -Asserttrue ($this -_testable -Trueproperty,"trueproperty isn ' t true");
}
Public functionTesttruepropertyisfalse ()
{
$this -Assertfalse ($this -_testable -Trueproperty, "trueproperty isn ' t false");
}
?>

http://www.bkjia.com/PHPjc/322881.html www.bkjia.com true http://www.bkjia.com/PHPjc/322881.html techarticle 1, marktestskipped and Marktestincomplete in PHPUnit, there are two useful methods marktestskipped and marktestincomplete. They allow you to write unit tests that are not only passed ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.