Testing Fundamentals

Simple Tests Exercise

Steve Kinney

Steve Kinney

Temporal
Testing Fundamentals

Check out a free preview of the full Testing Fundamentals course

The "Simple Tests Exercise" Lesson is part of the full, Testing Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to write tests for the subtract, multiply, and divide functions. After writing the tests, the functions should be implemented.

Preview
Close

Transcript from the "Simple Tests Exercise" Lesson

[00:00:00]
>> Steve Kinney: So our mission is to start with just getting the blood flow and getting some of these very kind of simple tests in place. I did addition there's not a lot of heady, how am I gonna implement these things, right? It is, you take the math operators, you make it work, right?

[00:00:17]
But the, what we're trying to do is get the rhythm of pulling in something, watching it fail, running the test, going and changing it, getting that rhythm in place and seeing it, and then we'll pick it up from there.
>> Steve Kinney: Again, we've got addition, and so now we'll bring in the rest of the function options along the way.

[00:00:40]
I could also import them all and then have those properties on here, multiply and divide, and we'll write some basic assertions of how this should work. So what I might choose to do here is again, I turn red cuz there's no test. Great it should subtract one number from the other seems like a good name.

[00:01:10]
>> Steve Kinney: And then we can expect, and if you see me make a booboo, you can be he must be working on some a higher level of thinking no, I'm probably talking while I'm typing, right? And it's probably a boo-boo. So if you see something like that, feel free to interrupt me because talking and typing while standing at a podium leads to mistakes sometimes.

[00:01:33]
Great, now, this one still fails why does it fail? Because that function doesn't do anything. So we can A, B, and then we'll return A minus B. And there is that rewarding feeling as the terminal beneath my editor just turned green for a second. Feels right and you might be I should go ahead and do multiply divided while I'm in here.

[00:01:59]
I'm like no, cuz we're practicing. This is about getting good habits in place, not the fact that you know third grade math or you know how to make JavaScript your third grade math. Okay, so then we do the same thing, get it nice and red. And like I did, I'm hitting command S, so I get that feedback as I go along.

[00:02:19]
There are people out there who have stuff auto save all the time. I can't do that [LAUGH] It's not me, it's not who I am as a person, you are welcome to do that. In fact, I think if I change away from this window it will try to auto save, which I did while I was teaching a workshop and someone said you should do that I turned that setting on, I presented it ever since.

[00:02:41]
It's never been the day I've turned it off. So, it should multiply two numbers. Okay, great and we go ahead, we put the test in there. And again, mostly, this is so that when you write tests later, you're not going, what is the syntax for a test? Because you've done it enough times in a row that all of a sudden you get the muscle memory in place.

[00:03:01]
And so again, we can expect that multiply anyone have a favorite multiplication? That's a weird ask
>> [LAUGH]
>> Steve Kinney: [LAUGH] Anyone have two favorite numbers they love to multiply? 3 times 2 is 6 it fails again.
>> Steve Kinney: A and B, and then we'll return A times B. Awesome, awesome, awesome we'll go ahead and it should divide two numbers.

[00:03:39]
>> Steve Kinney: And we'll divide, divide can always get weird on you, cuz you can choose to JavaScript, right? We can get to some weird places with floating point numbers, but we're not gonna do that to ourselves right now. We'll say, to 35, fails, and we get it working, right?

[00:04:02]
And that is the effective rhythm now, there's some edge cases here that we need to think about. And we will but let's get the test pass. Before I ruin our day already, I'm just gonna get the test passing. Awesome and that's kind of when you hear about like, test driven development that is the very simple version of it.

[00:04:22]
Obviously, depending on what you're doing, it grows bigger and bigger. And it is super useful for things like, hey, I got this one data structure, and I need to pull these properties off of it. Or I have this one data structure, I need to turn it into the other thing.

[00:04:34]
And maybe I need to pull three different properties and compute them in different ways very useful for that, right? But one of the things that I kind of wanna put in your head is at least give you a little bit of permission structure. When you can do this, it's a really great flow and rhythm.

[00:04:51]
And it's very easy to kind of get in the zone and kind of get that flow stay going. But like then, if this is so good, then why doesn't everyone do it all the time, right? Because sometimes it's hard sometimes you are getting existing code that has no tests whatsoever, right?

[00:05:09]
It's hard to do test driven development when the code's already been developed, and you're just adding a thing on top of it. It's very easy to get into the flow, but like sometimes you inherit code, and that's tricky as well. And there are a lot of cases where it may not work.

[00:05:21]
Sometimes you don't actually know what the problem you're solving is. You get that bug report, all right? And in a perfect world, a practice that I would love to instill in you is you get bug report, you write a test that fails, you make a pass. But sometimes you don't even know what it is and you're kind of playing around you're trying out different things.

[00:05:39]
And you find yourself 60% down the path of getting it solved and realize that you have not done test-driven development and that somebody out there is judging you.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now