From the course: API Testing and Validation

Write your first test

From the course: API Testing and Validation

Write your first test

- [Instructor] In our last video we wrote a feature description to test our environment. Now let's actually test it. Vendor, bin behat. And if it works as expected we should get a result that looks something like this. We have one scenario, which is undefined three steps that are undefined, and with this opportunity to generate a feature context down below. So we can go ahead and generate that and we can see it generates three placeholder functions for us. This resolves that undefined question we had just a moment ago. So we can go ahead and we can copy and paste this directly into our editor. We add these to the feature context file and now if all goes well, we can run that same command again on the command line and we should get a different message. This time the message was "fatal error class pending exception not found" which means behat itself is finding the methods it's supposed to, there's just nothing behind them there's no logic to fill them in and to actually execute. So let's go back to the editor and start filling that in. First, let's make a class property. So we'll make a protected variable called monkeyCount and we'll initialize that to zero. And now down below in the given I have monkeys function we'll go ahead and add that. Say this monkeyCount equals, let's make sure we cast this to an integer just to be clean about it. So now if we go back to our command line we should see something different. We run the same command that we did before and now this time notice we have a green line and the red line is the second line. The red line is the when statement this time, because we've defined our given statement so making a little bit of progress but we have to keep going. So back in our editor we go ahead and say, "I get more monkeys." We define this. So we say this monkeyCount, and then we can add we'll cast this to an integer and we'll add arg1 to it. So we're not doing anything particularly fancy here. We're making this as simple and straightforward as possible. We're just adding this new group of monkeys to our previous group of monkeys and now we need to check to see how many monkeys we have. So we can use a PHP function called assert and let's make sure our monkeyCount is equal to our line. And then we should put a useful error message here. So we can say "we expected this many monkeys, but found this many". And now if all goes well as we expected when we go back to the command line we should run and eventually pass this test. And it turns out we have an error. This is a common thing in development. So instead of asset, we should say assert. It turns out your code has to be correct to run correctly. Now when we run this we can see that this all passed as expected. So we've proven from beginning to end that our environment is set up correctly it's working as predicted. And in fact, if we want to test this we can go back to our feature and we can change it. So in our feature, we can say, "given I have two monkeys when I get two more monkeys, I should have five monkeys". Now we know two plus two equals four but what happens when we actually run this test? And you can see we got a fatal error. "We expected five monkeys, but found four." So we can definitively prove and understand that this test is working as expected which means our environment is working as expected. Now let's get on and build some real API tests.

Contents