[PHPUnit] I tried using DataProvider!
table of contents
Hello!
This is Fukui from the System Development Department!
This time, I tried using an annotation (a useful mechanism) called DataProvider that can be used with PHPUnit, so I would like to introduce how to use it along with the background and background that led to its use!
*In this article, we will omit the explanation of the annotation itself (what is an annotation).
What is DataProvider?
overview
Simply put, a convenient mechanism that allows you to use multiple arbitrary arguments in PHPUnit test methods .
*If you would like a more detailed explanation or explanation of annotations, please refer to the official PHPUnit documentation.
■ PHPUnit official document
https://phpunit.de/documentation.html
■ About DataProvider (annotations)
https://docs.phpunit.de/en/11.0/annotations.html#dataprovider
When to use
As mentioned above, you can pass multiple arguments to a test method,
useful when you want to use multiple different values for the same test method .
Specifically, let's say that there is a process to check the parameters of user information, and that ``login ID is required.''
At this time
・Null and empty strings are treated as abnormalities
・Response parameters are the same (same error response)
In this case,
strictly speaking, the test cases are separated, but the difference required for the test is whether
the login ID is "null" or "empty character" I hope this will be useful when implementing such tests.
So, I think it's hard to convey with just text, so I'll try writing the actual test code later.
How to use
actually write it
For now, I wrote a simple test code using DataProvider.
I would like to see if multiple values set in DataProvider (empty string and null in this case) can be obtained from test method arguments and at least pass the test.
namespace Tests\Feature; use Tests\TestCase; use Illuminate\Testing\Assert; class DataProviderSampleTest extends TestCase { /** * Abnormal: Test when no login ID is specified * @param string $invalidLoginId * @dataProvider invalidLoginIds */ public function testInvalidLoginId($invalidLoginId): void { Assert::assertEmpty($invalidLoginId); } /** * Login ID abnormal test data * * @return array */ public static function invalidLoginIds(): array { return [ [ ''], [null], ]; } }
The points of implementation are as follows.
- You can use invalidLoginIds() as a DataProvider by adding @dataProvider comment to the test method.
- invalidLoginIds() returns an array, but the elements within the array must also be written as arrays (like [''] and [null])
- The argument of testInvalidLoginId() is $invalidLoginId, but it can be determined freely (it is not intended to be singular).
Check test execution results
I was able to confirm that the test passed successfully.
Also, when I actually set the arbitrary value "Beyond Inc." and used the debug function (dd()), I was able to confirm that the intended string was stored in $invalidLoginId.
More practical usage
Up to this point, I have written the minimum test code using DataProvider.
However, since the above code is not very convenient, I would like to introduce some more practical ways to use it.
Below is the changed code.
namespace Tests\Feature; use Tests\TestCase; use Illuminate\Testing\Assert; class DataProviderSampleTest extends TestCase { /** * Login ID string confirmation test * @param string $loginId Login ID string * @param bool $isEmpty empty Expected value of () * @dataProvider loginIds */ public function testLoginId($loginId, $isEmpty): void { Assert::assertSame($isEmpty, empty($loginId)); } /** * Login ID string Test data * * @return array */ public static function loginIds(): array { return [ 'null must be Empty' => [null, true], 'Empty string must be Empty' => ['', true], 'testLoginId must not be Empty' => ['testLoginId', false], ]; } }
The main changes are as follows.
- Specify a key for each element of the test data (Specifying a test case or a value for the key makes it easier to see the execution results.I will attach the execution results later.)
- Specify multiple elements in the array (The above example specifies the login ID string and the expected value (bool) when executing the assertion.)
- Receive multiple values specified by data provider in test method arguments
Check the execution results again
Check the test execution results again.
The test passed successfully.
as I introduced earlier I think it
becomes easier to understand which tests passed Also, by passing a bool as the expected value, is highly versatile, including normal and abnormal systems
personal impressions
Frankly, I thought it was a useful feature for writing DRY code.
I felt that it was a good way to write test code more simply and in a small amount, so I would like to utilize it in future implementations.
Also, a little off-topic, but the reason I looked into DataProvider this time
was when I wanted to execute a loop by changing only the values used for a test in a framework of another language, and I thought, ``Well, PHPUnit has a similar function, right?'' ...! There is a background that led me to this idea.
It was a good opportunity to reaffirm that it is extremely beneficial to increase the number of implementation methods and tools that go beyond languages and frameworks.
There are other annotations that I have not touched yet, so I will continue to try them out.
That’s all for this article!
Thank you for reading to the end!
See you soon!
For web system/application development
(Click here for service page)