In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the usage of PHPUnit in PHP". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of PHPUnit in PHP".
1. MarkTestSkipped and markTestIncomplete
In PHPUnit, there are two useful methods, markTestSkipped and markTestIncomplete. They allow you to write more than just pass and fail unit tests. MarkTestSkipped prevents PHPUnit from executing a test method that has been written. For example, the following program:
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 database connection exception, you should use markTestSkipped to indicate that the test method should be ignored when throwing an exception, because there is an exception, and when you pay attention, it is possible that the code you wrote is correct, only that an exception has occurred In this way, PHPUnit will not simply output fail when it is exported.
MarkTestIncomplete is also a bit similar, but a little different is that it is used by developers when writing an unfinished test method, marking that a test method has not been written yet, and the test result will not be fail, just telling PHPUnit that the test method has not been written yet. The example is as follows:
The preliminary framework of the unit test code we wrote is as follows:
In the previous article, you introduced the setUp method and the tearDown method, where an instance of Testable () is created and stored in the variable $_ testable in the setUp method, while in the tearDown method, the object is destroyed.
Next, start writing some assertions to test, starting with assertTrue and assertFalase:
The assertStringStartsWith assertion determines whether the string begins with the specified string, and the assertStringEndsWith assertion determines whether the string ends with the specified string. The assertStringEqualsFile assertion determines whether a given file contains the specified character, for example, here determines whether the textfile.txt file contains the string foo.
AssertStringMatchesFormat allows the user to specify a matching pattern to determine whether a string meets the requirements, such as $this- > assertStringMatchesFormat ('% s match match test string).
Here is to determine whether $testString is a string type, you can refer to the PHPUnit manual for details.
Let's look at the following code:
AssertNull determines whether a variable is null, while assertSame strictly determines whether two variables are of the same type. Although it is a weakly typed language in PHP, it can still be determined that $numberAsString is a string type through assertSame, which does not match the expected 1234 number type, so the test cannot pass.
* Let's take a look at assertions that may not be commonly used, but may be very helpful to your unit testing work. Take a look at the following code:
The * assertion assertArrayHasKey in the code is used to check whether every key value exists in an array. For example, in our array, the value of "firstkey" has a key 1 corresponding to it, so the test can pass. On the other hand, assertClassHasAttribute can determine whether a class has corresponding properties, and in this case, the test can also pass.
AssertFileExists determines whether the specified file exists in the local file system. AssertInstanceOf determines whether an object you are creating is an instance of a class. AssertRegExp as you all know, this is to determine whether a string matches a given regular expression.
Thank you for your reading, the above is the content of "the usage of PHPUnit in PHP". After the study of this article, I believe you have a deeper understanding of the use of PHPUnit in PHP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.