Testing Fundamentals

Testing Randomness

Steve Kinney

Steve Kinney

Temporal
Testing Fundamentals

Check out a free preview of the full Testing Fundamentals course

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

Steve shares techniques for testing random or indeterminant values. These values include random numbers, unique user ids (uuid), and dates or times. Strategies include only testing the type or testing the value has the correct format.

Preview
Close

Transcript from the "Testing Randomness" Lesson

[00:00:00]
>> Steve Kinney: But it does kind of bring up a little bit of a different problem that we could kind of have to deal with. If I jump over into, I've got this example called characters. And let's squint at it for a second and see if we can find the issue here, which is all the testing stuff we've talked about is well and good until anything non determinant shows up.

[00:00:25]
Things that are non determinate, an incomplete list, the current date and time that tends to change, right, random numbers, right? In this case, a UUID is a randomly generated string, right? The goal of a UUID is that there's no such thing as true randomness, yada, yada, yada, whatever is that no two strings that it generates should be equal, right?

[00:00:56]
In the fullness of time, theoretically that is possible but it's like one of those things if you ran it over and over again for thousands of years you might get one but not in any kind of conceivable time period which is problematic ,right? Cuz here we are just generating an ID as part of this code.

[00:01:14]
Now I'm gonna spoil the end of this workshop now so that I can prove it at the end, which is you have three ways of handling this. Not caring about the parts that are random, that'll work. Making the parts that are not random not random, or controlling whether or not they're random, right, also known as all options.

[00:01:38]
And we're gonna pretend the first two are only on the table, and we'll talk about the third one later. All right, so the first one is just not caring about this ID, right? This can be a little bit tricky, because it's gonna be there. And to equal doesn't care if there are additional undefined properties, but if there is a new property that shouldn't be there, it is gonna yell at us.

[00:01:58]
Let's verify that, let's just look at this code real quick, put it in our heads, and then we'll switch over the test. Not a lot going on in this code. We've got this idea of a person. They've got a first name, and they've got a last name. If you forget to give one of those, it will throw, right?

[00:02:14]
And otherwise, we generate a random ID. That is prefixed with person, just cuz I felt like it. And we add the first name and last name to the object, and then we have this full name getter, right? If you've never seen this syntax before, you can just type dot full name, and under the hood, it will compute it on the fly.

[00:02:35]
Great, but I've got this test and it's technically skipped right now, which is because it's gonna fail. Let's watch it fail. I know you're happy with the green, but we're gonna lose it. We're gonna lose the green. So let's go ahead, we're gonna change it. We're gonna hit Save.

[00:02:56]
And it fails. But yeah, I made a person with the first name Grace, last name Hopper. That should equal a person, first name Grace, last name Hopper. Why? Okay, doesn't equal, that doesn't help. Look, we got this extra string, but it's gonna be random every time, right? Depending on what's going on, we could do something like, we definitely wanna make sure it has an ID, right?

[00:03:25]
That's important, that should be part of the test, right? But you can also do,
>> Steve Kinney: Nope, get rid of that 0 and this makes it a little bit more interesting, which is, hey, make a new person. And if I give you the first name Grace and the last name Hopper, right, I better see those coming out the other end.

[00:03:52]
However, as long as the idea is some kind of string, as long as it starts with the person dash, then you did your job. My code works as expected, right? So now you can see it does in fact pass, right? Now, if it wasn't prefixed at the right thing, it's only the random part where just like, I don't care about that part.

[00:04:16]
We'll just validate the parts that I control.

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