در حال بارگذاری ...

Snapshot Testing Overview0:00

In this video, I'd like to introduce you to snapshot testing and show you how you can use our phpunit snapshot assertions package to get started with snapshot testing in your PHP project. Simply put, a snapshot test is a kind of test where you don't write the assertions manually. Instead, you are going to compare the output of your function to the content of a snapshot that was written on disk. When you first run a snapshot test, the expected output is saved in a snapshot. The next time the test will run, it will pass if the output of your function still matches the content of the snapshot. And if it doesn't match, the test will fail.

Installing and Setting Up0:39

the content of the snapshot. And if it doesn't match, the test will fail. Let's head over to phpStorm where I'll demonstrate this. You're now looking at a test from our laravel-sitemap package, which as the name implies, can generate a sitemap for a Laravel application. In this particular test, I want to make sure that the sitemap rendered after we add a link to it is correct. Let's test this using a snapshot. Now, the first thing we need to do is to install the package, which I'm going to do now, phpunit snapshot assertions.

Now, the first thing we need to do is to install the package, which I'm going to do now, phpunit snapshot assertions. And with that package installed, we can add an extra trait, use MatchesSnapshots. Now you have access to the matchesSnapshots functions, and you can see we have a couple of those. Each of them is specialized in a certain type of input. Since sitemaps are XMLs, I'm going to use the matchesXml snapshot, and I'm going to give this function the rendered sitemap. Let's remove this, and let's run this test. So you can see in the output here that the test ran okay, but it is incomplete.

First Run Creates Snapshot1:46

Let's remove this, and let's run this test. So you can see in the output here that the test ran okay, but it is incomplete. It is incomplete because in the first run of a snapshot test, nothing is actually being verified. The only thing being done there is that a snapshot was created. And if I refresh my project view, you should see the snapshot directory appear. A snapshot filename always contains the name of the test it was created in. Let's take a look inside, and sure enough, here we find the rendered sitemap. And I can manually verify now if this XML is correct. Let's run the test for a second time.

Detecting Output Changes2:25

And I can manually verify now if this XML is correct. Let's run the test for a second time. Now the test returns green because phpunit now verified that the output of the function matches the content saved in the snapshot. Let's simulate a failing test. Let me go to the render function and just add something to the output here. This is invalid. Let's run the test again, and now the test fails, and it even can say that there is extra content at the end of the document that made this test fail. Let's go back to green.

content at the end of the document that made this test fail. Let's go back to green. Run the test again, and we're back at green. And it's very easy to add new tests. Let's add another one that will verify that it can render an empty sitemap. Let's mark it as a test. This time we are not going to add anything, we are just going to render it. I'm going to run the test. And the first time the snapshot is created, I can verify that this is indeed a valid empty sitemap.

And the first time the snapshot is created, I can verify that this is indeed a valid empty sitemap. And when I run the test again, we're back at green. Without using snapshots, I would be forced to put some XML in the test here themselves. And for a couple of tests, that is okay. But if you have a lot of tests, it can become a little bit messy. A thing to be aware of is that snapshot tests are a little bit brittle. Because all output is considered when determining if a test fails or not, even changes where your test isn't interested in, can fail your test. Let's simulate this by changing the Blade that actually renders the sitemap.

Updating Snapshots After Changes4:06

your test isn't interested in, can fail your test. Let's simulate this by changing the Blade that actually renders the sitemap. I'm going to change a small detail here, something that I'm not really interested in. I'm going to rerun all the tests in this file. And they all fail now. In the output here, you can see that the year has changed. If this change was intended, and you want to write this change into all of your snapshots, you can delete the existing snapshots and run the tests again. Or you could do that in one go by adding this string to your command. Let's do that now.

Or you could do that in one go by adding this string to your command. Let's do that now. So I'm going to run all tests from this file again. I'm going to update the snapshots, run the tests. All those tests are now marked as incomplete, because in this run, they didn't verify anything. They just updated the snapshots. And if we take a look at the snapshot, we see our updated year. And if I run the test again, we should get green. I think snapshot tests are great for smoke tests, or when testing big chunks of output. If you're testing short values, just stick to the methods that you already know and love,

When to Use Snapshots5:10

I think snapshot tests are great for smoke tests, or when testing big chunks of output. If you're testing short values, just stick to the methods that you already know and love, like assertEquals. If you want to know more about snapshot testing in phpUnit, head over to the documentation of the phpUnit snapshot assertions package on GitHub.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟