- Podcasts
- Programming Throwdown
- Unit Testing
About this episodeThis show covers unit testing, a way to put your code through the ringer before you show it to your users. Tools of the show: JsFiddle and…
This show covers unit testing, a way to put your code through the ringer before you show it to your users. Tools of the show: JsFiddle and towel.blinkenlights.nl. Books of the show: The Lean Startup (Kindle: http://amzn.to/157xbEl ), (Hardcover: http://amzn.to/12HwaDp) and Ender’s Game (Kindle: http://amzn.to/VcfVtD), (Paperback: http://amzn.to/Wg32hx)
★ Support this podcast on Patreon ★Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Unit Testing
No, I've been accused. All right, so my newest eccentric hobby is taking film pictures and developing them myself. That's pretty nuts. I guess film is kind of considered by many people to be dead, and long live the digital camera. And I do have a digital camera. I have a nice digital camera, an SLR, and I take pictures, and I enjoy it. And so it got me thinking, like, I want to do something, like, you know, different. I see these people, like, taking pictures on film and, like, all this special stuff, and it's kind of crazy.
And I began to be very intrigued because film went away kind of when I was just coming into kind of understanding it, right? So, like, I was still, I guess, probably, like, in middle school and early high school, and the first digital cameras that people had and were using started coming about. And so when you're that age and you're not into photography, like, I'm sure I'd taken some pictures with the 35-millimeter camera or whatever, and then just sent them off to the lab and they'd come back. And I always just thought this idea of, like, people having this dark room in their house, like, oh, this is, like, really intriguing. And, like, I would love to do that because it's basically chemistry.
So the way this works is I—so I got a really cheap plastic film camera, but it doesn't shoot 35-millimeter film. It shoots what's called medium format film, which is five times bigger than a 35-millimeter area of image size. So you can get really large negatives from it. But the camera is, like, this cheap plastic camera made in China. It's called the Holga. It's, like, famous for, like, being artsy, and it has, like, only two settings. It has, like, two different aperture settings and two different shutters, like, one shutter speed, and then, like, what they call bowl, where you can just hold it open as long as you want. Oh, okay. Which basically means your picture will probably be messed up.
Yeah. Exactly. Which is what happened to me a couple times. So I got this film. I got this camera. And, you know, I shot some pictures with it. And I'm like, I'm going to develop—and I use black and white film because the development process is a lot easier. And so it turns out the way that—maybe it's always been this way. But what you need is you need a darkroom to move the film from the camera to what's called the tank. So you have, like, this little tank, which is a black cylinder that has a lid on it and a very cleverly designed, like, funnel and center tube. And then it's called a reel where the film goes. So no light can get in even when you take the top off. Oh. So once it's in there—so you have to go into pitch black and, like, take the film out and load it into this little—so much engineering. It's actually really fascinating. But in the pitch black, you put it in this reel and you do this, like, twisty motion. And it sucks the film up into a spiral with space.
can actually see it. Nice. So it's terrible. I only got like two what I would call actual images. And then a bunch of blurry things and bad. And then a bunch of like overexposed stuff because I was doing this the completely non-engineer way. Like, I created all the variables at once. Oh, yeah. And so it was a new camera, a new process, new everything, right? So I had no idea what I was doing. It's pretty cool, though. But I'm pretty encouraged. Yeah, so I got one picture that I thought was like really good. And one was underexposed. So I've got some problems. I'm working through them. This is fun.
So what I did was shoot the film, develop it, let it dry, and then I scanned it on a scanner. And I don't have a fancy, nice scanner or whatever. But, yeah. So I'm going to try to improve it, try to be better. And I'm doing this not because it's cheaper or, like, I think it's more awesome. It's just interesting to me. I should be more eccentric. So, chemistry.
Jared, I'm not talking to you anymore. So on to tech news. On tech news, we have Flat Pixels, which is—it's actually sort of a blog post, an article on different design patterns, especially with mobile. And I thought it was kind of interesting. It actually goes over a bunch of different, like, competing motifs in mobile. And so it kind of talks about, you know, Apple's known for having that very texture-based feel. So even—Skeuomorphic. Is that what that's called?
Yep. So Apple's notorious for that. And they've almost—according to this article, at least, they've almost overused it. And now people are wanting something that feels more like I'm not in the real world. So they want to take something that feels very more abstract. And so you're seeing this shift. First, you had this shift towards realism. And that's one of the things that made the iPhone so cool was because it felt like a real thing. And, you know, you have the Kindle, which sort of feels like real paper and things like that. That was sort of the trend. Or sort of looks—not feels. It looks like real paper with the E-Ink. And now you're sort of switching back to this sort of Tron-esque, you know, let's go back to the very, like, 8-bit, retro, you know, blocky style. And so that has its own appeal. And so this person, I'm assuming, is a good designer, but basically—at least they're an educated designer. I don't know anything about their work. Well-spoken, maybe. Well-spoken, educated. Kind of sort of discusses these different paradigms. And I thought it was a pretty interesting article. So give it a look.
Well, it turns out everything is skeuomorphic in this blog. But they're skeuomorphic for different reasons. Oh, okay. So the one on—so we're actually looking at two pictures from the blog post. The one on the left has just very solid flat colors for buttons. Oh, so it doesn't look like buttons. They're just squares. Yeah, exactly.
Yeah, the other thing they talk about, too, is there's a tradeoff. So if you—you might feel like I want it to look like the one in real life. But on the flip side, if it looks like the one in real life, you also expect the same limitations of a real-life calculator. Oh, interesting. Yeah, so they actually argue both sides. So the calculator on the left, the one which just uses flat buttons, if it did something, some really high-level math, or if it took you to Wikipedia or something, you'd be less jarred than if the one that looks like a real calculator did that. Makes sense. Yeah, so it's sort of a really interesting article. And then it goes into Apple and Google and all the big—Microsoft, all the big shots, their different UI techniques and things like that. And so I thought it was a pretty good article. Yeah,
Okay. So the one I have is not like that at all. It's about a version of Minecraft that was released for the Raspberry Pi. We've talked about the Raspberry Pi before, the really cheap, small computer that runs Linux. And they released a version of Minecraft for it that's for free, but it's stripped down. It doesn't do everything or in the same way. And I guess maybe I didn't—now I'm trying to remember when I read it. I think maybe also some of the source code or at least the ability to kind of extend it was released.
I played a little bit of it. I thought it was pretty cool. Well, actually, the coolest part for me was—did you have this thing where you could have a website constantly sitting in your server and then streaming to the web? No. So, yeah, my friend had this setup where basically we'd have this God's eye view of our town at all times on this website.
So while we're on the topic of gaming, and maybe you could use this in Minecraft to be cool, but I saw some stuff recently, and it got me thinking about virtual reality. So the Oculus Rift, which was a Kickstarter project, which is actually turning out. It looks like they're going to do really well and kind of deliver on their promise. But they're trying to make a kind of affordable virtual reality headset with the accelerometer that, you know, can tell where—like, when you turn your head. So when you look to the left, the view of the world pivots to the left. Oh, okay. So it looks like you're there, right? The whole virtual reality. So I've used virtual reality goggles before, but they were really, really bulky, and at this point I've
right. So they had, like, a demo where I guess they're able to, you know, integrate with a lot of the current games without much modification, which is really awesome. Oh, yeah. That's sweet. Somebody was telling me you could have done this for a long time because basically you send the models to the graphics card. So the graphics card—the graphics library calls already know the distance to objects when it's doing its transformations. Yep. So when it does that, it can go ahead and just essentially do it twice and give you the 3D view. Yep. Which I did not know. It's kind of like mind-blowing. Yeah,
So these goggles are, like—people are, like, wow. Like, you see people in the video. It's, like, oh, this is amazing. And it's hard to get over, like, the way we're used to doing input. So the way they have it now is kind of, like, the goggle is mapped to what would be your mouse in, like, a first-person shooter. So if you,
your head to the left and then you hold the W key, you start going in the direction you're looking. And so I guess people have a little bit of trouble with that, but it seems like you'd get used to it pretty fast. But I really want to try it. Yeah, I totally want to try this. Like, watching a video is, like, this is not—doesn't do anything other than it makes me want to try it. I don't know that I want to buy it yet, but I definitely want to try it.
was saying, like, people kind of gave up on, like—Yeah. I guess everybody has 3D TV. It's another thing. You can't not have it, maybe, is the way it's going. I don't really know. But they're going to other stuff now, like other novelties. Cool. But, yeah, 3D TV doesn't strike me. Like, I don't want to sit in my living room when I'm doing other stuff, wearing funny glasses and looking at the TV.
exactly. I mean, a game is made for one person per terminal. A TV is made for many people per terminal. And in this case, like, just personally, like my wife and I, we almost always watch different shows. But we'll watch each other's shows to give each other company. So for a given show, one of us will be paying half attention, right? And so to have to wear 3D goggles would just make it just a really painful experience.
in JavaScript? That would be pretty awesome. I should look up for something like that. Something that teaches you to play the fiddle. All in JavaScript. All in JavaScript. Using that HTML5 audio. In ASCII art. Oh, man. We'll get to that. But JSFiddle, it's pretty cool. Basically, you can write some JavaScript, and it has a little drop-down where you can choose any of, you know, a number of frameworks. So if you like jQuery, if you like AngularJS, MeteorJS, whatever you like, it's an option. So you set this option, and it gives you this four-panel setup where one of the panels is the output. The other panel is, I think, HTML, and the other one is JavaScript, or it's, I think, CSS. And the idea is with one of these, you know, with one of these types of files, with one HTML, one JavaScript, and one CSS, you can effectively do anything simple. So wait, it's an IDE? Sort of. Okay, I'm a little bit lost. It's actually meant for showing things off to other people. So you might have this cool JavaScript rollover, or you might have this interesting way of binding data, or you might have this, like, cool little neat thing that you want to show people. And rather than use something like Pastebin, which just paste source code, you can actually, you know, type it in this JSFiddle or copy-paste it, and it will show you the output. So, for example, I wanted to do something where when you clicked on a button, the page actually slid off the screen, and the page that was meant to replace it slid in from the left. And somebody wrote a JSFiddle to do this. So you just give it a couple of divs in HTML, and it'll handle the sliding and all that, and it worked really well. And you could really test things quickly. You could iterate quickly. As soon as you change some of the code in one of the panels, the fourth panel refreshes. So it's kind of a neat little tool.
Sounds cool? Yeah, I think I need to, like, I think visually I have to go see it. So, all right. But if it's me, then it's not going to be looking at it and telling you isn't going to be very fascinating. So we'll go to mine. So mine, I feel like I can do a good job of having you visualize this in your head. But if you go see it, it'll be so much more awesome.
library. So you—oh, now it's okay. All right. It wasn't a web development problem. It was a Patrick's brain problem. All right. So mine is the awesome, amazing thing. And I think I've seen this years ago, but we're talking about it today because somebody had posted an IP address that if you do a traceroute to it, the host names give you the introduction to Star Wars. Nice. Which is pretty nerdy. And I guess they don't actually have that many computers with those host names. There's some trick that they do. There's a couple different ways to do it. But you do need a lot of IP addresses, I guess, to do it. I thought that was interesting. It reminded me, and I found it. But there was a website you can telnet to that does an ASCII animation of the entire Star Wars Episode IV movie.
Scrolling up and the first scene, you know, with Princess Leia on the ship and Darth Vader coming in. And I started to show this to Jason and he began to say, 'Okay, how long is this?' You know, I don't—I'm like, I think it's as long as the movie.' So I think they literally every scene, it goes by, even like the opening parts or whatever. And so it's really fun. If you've never been there, you can look on the website. It's towel.blinkenlights.nl with a K. I don't think you're going to—you can search for it: Telnet Star Wars Episode IV. But I guess somebody had created ASCII animation for the whole movie in like a Flash thing or something. And then somebody got all of that and set it up on a Telnet server. But I haven't watched it, so I can't verify that it's the whole movie, but I'm pretty sure it is. Yeah.
Yeah. I'm not sure. That seems like I would give up after the first scene. Yeah. I'll be like, 'How do you draw this bun on Princess Leia's head?' And then just the ASCII movie staring at me. Yeah. I would just give up right there. So it's pretty awesome. This guy did not give up. He made the entire movie. We think. In Telnet. Or at least the internet says. A lot of it. At least the beginning, the beginning half an hour. There's too many caveats. It was pretty epic. You guys should check it out. Book of the Show.
Is that our newest? I think it's our newest segment. Oh, oh yeah. That's right. Our newest, our still newest segment. Unless the question of the show. But we didn't even do that. Okay, stop. All right. Book of the Show. Book of the Show. So my book of the Show is The Lean Startup. And it's a book that a coworker recommended. It's about dieting? It's about dieting your company. But I don't really know what that means.
Process standpoint. I'm sorry. Okay. I'll stop. The Lean Startup. It's basically, it's from a guy who spun off a lot of startups. So people who don't know, who aren't from Silicon Valley or from a similar area like Seattle or something, has a lot of startups. A startup is effectively a group of people. They're usually called the founders, who have a new idea, something novel and interesting, and they want to take a risk on this idea that most big companies wouldn't take. So for example, you know, if you want to make some kind of crazy video game where it's a crossword puzzle but based on Google Maps and you play people in your city or something, right? You know, most new companies, they like doing things that are sort of tried and true. Or they like buying startups that succeeded, right? So they'll pay the extra money to buy something that's already a good idea. If you have a brand new idea, often you have to, you know, spin off what's called startup and try and tackle these ideas and find out what value there is there. So it turns out that, you know, going from a cool idea to something real that is sustainable and a real business is an art form. And so this guy who's done a lot of that and has a lot of experience shares that experience in the book, The Lean Startup, and talks about what it's like to deal with team dynamics and all about being able to capture information. So the first time you try it, you might make a crossword puzzle and the system can't handle the load and dies. And half the people who play it have this major complaint of one or another. And the whole thing might be a flop. And you might have to scrap the whole thing. But you still learned a lot from the entire experience. And being able to learn quickly so that the next thing you make can be that much better and being able to make sure that you learned everything you could learn from each iteration of your product is what most of the book focuses on. I think it's a pretty awesome book. I'm almost done with it, and yeah, I definitely would recommend it to anybody.
to that really soon. And he used some examples. One is Zappos.com, which is now a huge site that sells tons of things. It's owned by Amazon now. Oh, really? Yeah, I believe so. It's still pretty big, even though it's under the umbrella of an even larger company. But the idea with Zappos, it started as just a way to buy shoes online. And the way the guy actually started, he had a little bit of experience with shoes, but not much. He was mainly a software guy. He actually went to the stores. He took pictures. So he went to Macy's and even Walmart and Target. Took pictures of their shoes with his camera. Put the pictures on his computer. Made the site Zappos as if he was selling the shoes with a little bit of a markup. And then people would buy the shoes. He would go to Walmart and buy the same shoe and then ship it to them. So he wasn't doing any of the distributing or anything. And that's actually how Zappos started. And that was the minimal viable product. Now, the reality is with Walmart or Target or wherever he was actually buying the shoes from, doing all of the distributing and warehousing, he didn't make a lot of money. In fact, he probably made no money. But he developed a huge audience of people. And he showed that this was a successful business, that people would buy shoes online, even though they're worried about sizes and things like that. So he had to deal with a lot of issues. And rather than try and get shoemakers on board and do all this craziness, he just said, forget it. I'll buy the shoes myself and ship them to them. And I'll still learn a lot about selling things online.
Right, right. Yeah, and so being able to do the part that's interesting and cool without the things that you might think are required is a big part of the book. Interesting. Yeah, it's super interesting. Yeah, check it out. Someone else had sort of a Q&A site, kind of like Yahoo Answers equivalent. And he actually answered almost all the questions personally under a bunch of synonym accounts until it got bootstrapped. And so this whole thing is very fascinating, right? Wait, did he
Oh, well, that happens a lot because it's just forum code that someone's duplicated. But I'm sure that even when the first person went to Yahoo Answers, there were probably a bunch of internal questions and answers maybe that were there. There was content. And so the whole thing is fascinating. I think it was a great book. Yeah.
Yeah, me too. I really like this book, though. It's considered a young adult book, I believe. So I think even younger kids can read it. There's nothing super violent or bad in it. But it's a really good book about kind of like a misfit boy who is kind of the unlikely hero of a story. Kind of that is the trope it plays on, or whatever in a way. And, yeah, it's interesting. I like it. It's a classic book. You know, probably 20 years old now, 30 years old. Something
And so it's been out. A lot of people have read it. But it's got space in it. It's got technology in the near future—not distant future, but like kind of near future. And it's got aliens. Yeah, it's pretty epic. It has all the ingredients. Okay, this is actually pretty hard. So you can talk about the Lean Startup book and like talk about what's in the middle and the end and what it talks about. But I feel
There are many books in this world, I guess. Right. So this is the first book and it's standalone. So if you just read it and end like you—I mean that's fine. Yeah. That is not a problem. But then I think it's a series of five books going forward. So there are four books that follow after this book. And I can't talk about what's in any of those books because—yeah, dude.
So it's the same storyline but it follows other characters who are in that story. And it's like from their point of view. So they're the main character instead of the main character of Ender in Ender's Game. So it follows them and kind of what was happening while that storyline was going on. And so those are the Shadow books. So it's like Ender's shadow, and then
And so even for people who have read that, sometimes they don't know these other books have been written and are still being written, I guess. I think he might still be working on it. Wow. But overall like those books are really good as well. In fact, some people say like people I know have said they enjoy that—those books more than the Ender's Game book.
Yeah, but yeah, the book is awesome. I believe it's one of these things that will become a classic and become required reading and things like that. It'll be like the next Watership Down or Pride and Prejudice. No. You never read – oh, I know which one. Count of Monte Cristo. You must have – really? Not everybody in Florida had to read that book. I mean I know
Gente revelation here and oftentimes it's kind of lost on me because i get really into the surface of the book so i'm really into you know as we talked about this book space battles and things like that and the things that are happening to him very directly and um um it's hard to get the bigger picture but i think this one sort of punches you in the face with the bigger pictures do you think
long time no i think it's a story i don't know if it's actually a book so this is a story about the i'm just going to paraphrase and skip all over the place but to get to kind of like the point of the story is that there's an emperor who's very proud of himself and this uh crook comes and basically tries to sell him clothes but it turns out he's not actually selling him clothes he's just pretending to make him clothes but the emperor is really naked because the guy didn't make him any clothes and so the emperor's parading down the street in his new clothes which oh if you if you know what's fine and you're fancy you'll see the clothes that's what the guy kind of tells him so he's like oh of course i see these
also says like oh yeah them and finally a little kid goes you know like wait you're naked so this is like the story the emperor's new clothes or whatever so i'm kind of like about some of that stuff that people say i believe there are some authors who say like oh i want to tackle this issue and i'm writing this story and i think other authors just say like let me write this story like i want to write a good story and then people like try to build all this stuff on top of it and like make
The author's trying to say this. Yeah, wait, maybe the author's just saying what's most interesting to write here. Yeah, right? And then it's less of a commentary, and I've heard authors who kind of go on both sides, but you know, I think sometimes these things fall into—and this is off topic and tangent, but it's okay—so I think sometimes it, I call it Emperor's New Clothes, right? Like people
Yeah, I see that too. Yeah. Then you feel—yeah, you feel like peer pressure, right? To agree with everyone. But yeah, I feel even more so with art. This lady will be like, 'This lady will say, oh, you know, this red color really means that he was angry at this point.' Like maybe he just had red. Maybe red was on clearance. Come on. You know, but it doesn't lessen the
think people try and take it too far so that they can iconize that—that icon, icon, iconize that particular word, and so they want it to be unique. They don't want someone else to basically copy that style, so they say they attach all this feeling and sentiment, and they use 'sumo.' Only you know one person can; this other person can't have the same feeling as this guy,' because I think sometimes like reviewers on Amazon review saying like the first
apple fell, and you learn next time to build an umbrella. I don't know, but the idea is, you know, Patrick and I are big fans of unit testing myself, especially. And, uh, you know, this is one of these things that even when I was in college—I don't know about you, but I always thought testing was kind of for suckers. And oh, it's one of these things that sounds cool. Kind of wait, wait, my
it always works the first time. So yeah, I mean that's the thing. You feel like, oh, you know, doing tests is like doing proofs. You know, the reality is, you know, at least in Machine Learning—I'll say, I can say with authority—that almost all proofs are BS. Right? The reality is most algorithms require some kind of fudge factor or a bunch of knobs to turn, and you have to kind of get that right. And there's all sorts of corner cases where the algorithm fails. Right? The algorithms that provably converge are useless, and things like that.
kind of took this—took this sentiment over to unit tests and said some of these things. It's cool to talk about in class, but we'd never use it. Right? Turns out that unit tests, as we'll see from their history, have evolved to porn. They're now extremely useful. Um, so the very first unit tests—they call this the era before 1956, the debugging period. The idea here is if you said I was writing a test, what it really meant is you're running your program and trying to see seeing what
They had GUIs. Maybe. Yeah. Okay, you run your program, make sure it does what it's supposed to. And then that was followed by the destruction oriented period, and this is where people kind of felt like testing should find errors. So the idea is in this phase, you'd compile, you'd—your code would compile, and then your code would test, and then your code would run. So you would write tests that would do things like run a function and give you an expected result, the very sort of what we consider now is the simplest possible unit testing was started around the—you try to break.
Fail. Yeah, exactly. So then there's the evaluation period, and this sort of takes it to a higher level. This says that testing should actually measure the quality of your application, and this went on from like around '83 to '87. And so the idea here is maybe you're writing a bigger app or a distributed app; you have many users, things like that. So you want your testing system to sort of be more reportive. You want it—you know, maybe every now and then are in a situation where something fails, you know, you're writing big apps which parts of that fail constantly? Let's say you want to just get an overall feel for the quality of the app, so is it performing 90? Is it doing 90 of what I want it to do? Maybe of these two or three features which are either completely broken or semi-broken but most of the apps operating the way you'd expect. And then now we have the prevention oriented phase. And so the idea now, the sort of mindset for people writing tests now is that the point of testing is to sort of detect and prevent faults from occurring in the system. So this involves sort of an amalgamation of everything, all the other periods combined. So testing should be part of your runtime framework. It should be something that you do almost as an extra compilation step and everywhere in between. So a unit test—I mean, we.
Say it a unit, but what's a unit? Like I guess that's subject to definition, to different differences of opinion of what a unit is. So I worked a job once where unit testing was like kind of what we talked about in the history here—that you run your program and it runs on a set of input and produces a set of output, and that you're unit testing your thing, right? Your program, right? Your whole program, you know. It's some or or you're at least you know you're testing like a set of a whole set of classes and stuff all together at once, right? You're testing that they do exactly what you define, and that was unit test. But I don't think that's what unit test is really supposed to.
No, no, no. We won't cover all of them because that'll take forever, but we'll give one sentence of each of them. Unit tests are at least is my opinion, and you could—you know, feel free to correct me or bounce ideas off. Dang it. Unit tests are testing for example one function. You have some function Foo, and it's supposed to take A and transform it to B, and you call Foo a bunch of times with expected B's, and you give it some A's and expect B's, right? For Foo, I would defer.
So in other words, if you have something which isn't doing any decisions—it's, you know, it's not doing any kind of math—it's just like very basic function, like a get method, right? Some people are like, 'Oh, you need a unit test for a get method.' Well, I mean, if there's a problem there, you've got worse problems. Now, a set method, if it's doing some sort of checking that may need unit tests, but in general, like, oh, you know, writing a unit test for just a simple set and get method that doesn't have any sort of balance checking or any of that, it's probably overused. So saying function maybe not—maybe you're calling another function which calls that function. Yep.
That's not a size change though. Like a next thing would be like go to maybe like a system test or an integration test. Like so, like going up the scale, this is interesting. So you can kind of like go up different dimensions, right? So like you can go from like a unit test to like testing multiple units together. That's
integrate? No, integration test. Oh, it's integrating multiple things together—and multiple—and then you get to like your whole system which is composed of subsystems and parts. And depending on the size of your project or the scale, you may have many levels in between. You may define them differently, but then the system test is like kind of testing the whole, you know, everything all up together that it runs. It, you know, given an input there may be many, many, many, many, many calls down and back up and decisions, and then—and then it returns something, and you're testing the whole system at once. And that's kind of where you typically requirements are written against the system, and so you're testing that you meet all the requirements. So what's a regression
math. Yeah, and uh so you take the numbers and you run them once, right? And like with your current system. Then you want to apply this change, but you need to prove that the change doesn't break some of these core tests. So it's not—so much that's why I think it's a different dimension because you're not testing more or less; you're testing it from point A like time interval zero. Now at time interval one, do my results match? Or can I explain why they don't? Oh.
Worse. Yeah, yeah, exactly right. So okay, okay. So there's integration tests where—that's more for where you have, say, a set of functions and you have expected output for that function. Maybe that output is in a file or something like that, but it's not the typical output your system might deal with. Files? It might be some web service, but you have an input file and output file just to test this core piece. And so that would be an integration test. Um then, as Patrick said, the higher the highest level is a system test where if you're making a website, you would actually have—so there's something called WebDriver where it can actually start up Firefox and click buttons and tab and type things in input boxes as if a real human was doing it. And so that's the highest highest level in the pyramid, right? So system test is where someone literally you bring up a test server with your website on it, and then this motor—this computerized agent goes on and types some fields in and registers an account or something like that, and that whole process should succeed. So then you
Can get into even another dimension: you get black box white box testing. So black box testing may be like, oh, you bring up the fake version of Firefox, you type this stuff in, then you try to log in once you've created an account and you make sure that works because a black box treats the whole system as a black box. So I don't know anything about it. So this could be done by an outside person—a you know, a contractor who's not working on developing this source code. Could be done pretty much should be by anybody, right? Who just understands how the system is supposed to work. But then other tests like unit tests can only be white box tests. Oh.
whoa i don't know that's hang on hang on but so i think that one of the kind of ideas there's many many ideas in many different spectrums but one of the things about unit testing is if you kind of go down and you say i'm testing the lowest level and i'm making sure that that's correct then you can build up because if you then when you do an integration test you're not trying to retest what you tested in the unit test right which is a common mistake i think when i was starting out that's what i did a lot like oh i need to retest at the same thing like run the same test but you don't really what you're testing is interfaces so i assume people have unit tested each of their individual parts and now i need to test how they interact together
To write an integration test, for instance, like you had an RPC—a Remote Procedure Call—and you know has a client and a server, and then it executes, you know, some of these two numbers together. You don't want your integration test to have the purpose of, 'I'm writing an integration test which has the client call the server with the numbers one and one, and then I get back two,' right? For the purpose of testing that that bottom function is doing one plus one equals two—I mean, you do need to write probably a test that, you know, makes sure that you get back the right answer, but you don't need to go through all the corner cases of the bottom-most unit there. You need to test all the different ways of calling; that's what you need to be focused on.
On, yeah. So I mean a better way to do that, just finish Patrick's point, is you could have—you could make the RPC with one and one, and you could call the function directly with one and one, and in both cases you should get the same answer. And the nice thing about that is, I mean, in the case of adding it's obvious, but let's say you change that function, like let's say one and one really is now equal to three for whatever reason that logic has changed. You don't want to have to go back and change your integration test to make—to change a two to a three. You want to change your unit test of that function, and then the integration test should just should still just work fine. So that's
going to spin for a long time. Yeah, so we kind of talk about like one of the things that you—there's this very beautiful notion in computer science of getting to provability. Like I want, you know, I want to treat my algorithms like math and I want to be able to generate proofs and have provably correct statements. And to some extent, we get there; we don't get there. But the harsh reality is most of us don't have the time or freedom from our management to get to the point where we can prove it do provable correctness for our code, um, and also the systems we run on and the environment run on don't even allow for that, even if you wanted to, right? But unit test tried to help us get there. They say what are all the possible ways to kind of use this unit, this base of function, this minimally viable piece of code? Like, and what are all the different ways to test it? And if I test all of those, then I should have provable, you know, at least to some level. I should be able to prove that it's correct that people can call it and assume it's going to work, right? Yeah. And and that gets to an interesting point about—and we'll just gloss over this about whether you should write the unit test before or after you write the
code. Yeah, I've heard both ways, and I actually started writing the tests after because that's sort of more intuitive thing. And plus, you want to get the cool thing done, right? But I've found lately I've been writing the tests before. And this gets to something else I think is really interesting: is when I just—to finish the story—when I started, I thought tests were boring and kind of lame, and no one really used them. But now I realize the cool thing about the test is that you could put your functions or your system under a set of like a under a state of distress that it will never see in the real world. Well, that it would be very difficult
just and to be able to see that is really cool. So for example, um, you might write something that, you know, reads and writes records like the ZODB, for example. I wrote that Zombie database, right? And the cool part really was writing the tests where I had 128 threads concurrently trying to read and write elements and then checks to make sure that the database wasn't corrupted, right? That was really cool. And the first time I wrote it, it completely failed the test—I mean, completely. Like the first record crashed the system, right? And then slowly I got it to the point where it was more and more robust, and it reached a level of robustness that in my entire web app in that Trivipedia nobody would have ever like I would never have the type of bandwidth that would have caused these issues to come up. But making something that was robust enough to handle these scenarios was actually really interesting and fun. And so the writing test part was actually one of the funniest parts, even though it seems—I need tests for my grammar? I'm sorry. It's one of the most fun parts, even though it's, you know, canonically it's stereotyped as really boring and mundane. So, so we
know check that something is null, but then later you also check that it's null like that it hasn't somehow not been null again because you feel like for sure that's happened to you once. Yeah, and in reality it was something else you had messed up. But you know, you really like you keep checking and checking so you can write, you know, like an insane amount of, you know, lines of code trying to test the same thing. And so we talked a little bit about corner cases, so, you know, kind of the method is you're supposed to test the different kinds of tests that are the corner cases of what you do, and if you test those, that should be sufficient. And that kind of interplays with another thing which is code coverage, which is saying you should be able to execute every line of your unit when you write your unit tests because if you don't, you could be a sign of problems. One is, how do you know that code that isn't tested is going to work? You don't have a test for it. Yeah. And the second thing is if there's no way to reach it, then it shouldn't
And I've had this before. Like through one way or another, it turns out there's an IF statement that because of previous IF statements and exception checking and handling and throwing, you can never reach that set of code. Yeah, but you put it there because you weren't sure, but now, like going through the unit test makes you realize you can't construct a test case that ever exercises that. Then it should just be removed.
Yep, yeah, definitely another cool thing about unit tests is one of the strengths of unit tests is that they actually force you to write better code. So for example, one of the when I very first started really getting into writing unit tests, I kind of sort of decided to just jump into the frying pan and unit test this code that I had just written. I was like, 'I'm going to do this and find out how this works.' And I realized that my function didn't have any inputs or output. It had one input, which was just a file that it read data from. It had one output, which was the name of the file that it output to. And I realized that it was impossible. And it was one function, and I realized it was impossible to test this function. I mean, I didn't—it didn't even make sense because it was such a black box and it had been designed so poorly. So the first thing I did was I took the function and broke it into pieces, and I said, 'Okay, this piece you know inverts a matrix.' And so I can I can invert—I don't have to invert a matrix from a file. I could just give it a list of numbers and tell it to invert that matrix. And this other piece does something different, etc. And it sort of it got better, but in the beginning, my very first unit test took, you know, 10, 20 minutes, and it actually wasn't even a unit test; it was it ended up being a system test, right? So so take
that example a little further, I think, because it gets into the other point: writing testable code. So like that's very important. It makes you write your code differently. But then to go a step further and we're going to talk about where kind of mocking comes in. So so Jason's saying he has a function which reads from a reads a file that the path is given to him, reads a matrix out of there, and versus the matrix and writes it back out. We'll just say that's the that's the code he wrote. So we wrote all of that in one function to start testing that is like near impossible. Yeah, exactly.
can do and say he's talking about breaking the inverting the matrix function out. But now he has code which takes in a file path, you know, reads the matrix—calls reads the matrix that sounds awesome—reads the matrix, styles the operator calls the inverse operation, gets the results, and then writes it out to a file. So that over that parent function there, the one that's that's still doing too much, but also that's also really difficult to test. And you don't want the whole kind of one of the goals of unit testing is you don't want that test that tests that function to also be dependent on the tests for the matrix inversion, right? So instead of calling the matrix function by name, Jason could create a class which encapsulates the matrix inversion because then maybe he has different kinds of matrix inversions depending on whether it's a very very big matrix or a very small matrix or a certainly shaped matrix or a sparse matrix. Yeah, so all those could be different matrices. So now he has a class which represents that, and instead of having to go create a test instance of that, which doesn't do anything meaningful, you can use a mocking library. When a mocking library is a way you can you can create mock objects on your own which just say 'Test Matrix Inversion Class,' which doesn't do anything other than just return the matrix you got, right? And you can write that on your own.
what a mocking library does, and they have these for many different languages, but the kind of the root thing is always the same: it allows you to create a tell it like, 'I want this object, but I want it to be fake,' right? And then when it's called, just return this. Yep. And then once once you run your test code, you check and say was it called in this way and how many times was it called and what was it called? You can kind of do all those checks, and then you check that it was called appropriately, that was called correctly, and you're not dependent on that. You're not dependent on the implementation of that class, just on the definition of the class, the interface of the class, which is all you want to be dependent on.
Yeah, so getting to Patrick's point. Let's say you just had a file that let's say each line was supposed to have a matrix on it. So you gave it an input file with three lines. The matrix inversion function should be called three times. And the mock what it will do under the hood, it'll actually have a counter for how many times the function was called and it'll tell you, 'Hey, you actually um the very last line of your file was just an empty line, but you tried to process it as a matrix.' So your your matrix inversion got called four times instead of three. Or something like that. It'll actually—that the test will fail for that reason. But
then then the function still is not to keep to go this example a little farther unless people think we're not doing justice. You still have this call down to the OS there which says, 'You know, open the file.' That's still problematic because you don't want to do that during a unit test. Yeah, right, right. And also in general, like as a you know engineer, you shouldn't be doing that because what happens if somebody wants to port that from one operating system to another? Yeah. So then now you see where this is going. Now instead of passing in a file file string, you know, you need an object which represents how to open the file so that you can abstract that away. So you can kind of begin to see how you have Object-Oriented Programming, unit testing, mocking—they kind of like are swirling together here. Yeah, and you can be poor at some of those, but if you really get involved in starting doing the whole process, it's going to make you better at all of
think so. I thought unit testing was a waste, and then I started doing it, and I was bad at it in the beginning, and then I got better at it, and then I learned—I really you know, you have these things you hear like separation of concerns and testable code and you know all these things, and it really like it began like, 'Ah, this is just people like you know just wasting time.' And and fuzzing, but in reality, like once you learn to implement all those things, you learn why there are those design patterns and why things are important to be written in a certain way. And you
build better code. Yeah, and historically the I look at the projects that have that I've worked on, side projects that have succeeded and failed, and real work projects too, and I've noticed a trend that especially with my side projects, they get to the point where they're not easily testable. Like whether I'm writing unit tests or even before when I just tested it by by running it and seeing what happened, the point where they stopped becoming easily testable is right the point where I gave up. So for example, I made this hockey game, and the hockey game came out pretty cool, but I had bugs such as you know at the end of the intermission in between quarters, it I would sometimes crash, or the statistics for many games after you play 20 or 30 games, the statistics were not being calculated right. Bugs that required you to play 20 games because I had no tests. Um those things when I started encountering those bugs, I kind of was paralyzed. I mean, there was a bug where after 20 minutes it crashed, and I would literally play for 20 minutes, wait for it to crash, and see if I could catch this bug. I did that a few times and then gave up, right? So a lot of these projects the testing is sort of what can keep you sane when you're going through it and kind of keep you motivated, and plus it's pretty cool to write a test and to actually see some bit of code do something meaningful even far before you might.
That is a problem with the system test, right? So if you only do system testing your way to the end, there are so many potential problems. Like you spend tons of time debugging versus if you have unit tests, it kind of tells you where you—where the likely culprit is. If all your unit tests are still passing and then that goes to another thing is like when you do find where the problem is, you know, because you might still have to kind of go through all your code looking for the problem, but you finally find your problem, you should figure out why a unit test didn't fail. Yeah, and write a new unit test to make it fail. Yeah. So we have this procedure where I work now where if somebody has an issue and it caused a problem until you can't say you're done until you wrote a unit test that makes that problem, yeah, you know, appear and show that it's like as part of the fix, you have to make a unit test that failed before the fix. You implement the fix, and then now the unit test passes, right? So to prevent somebody from getting into that in the future. And also for the other another good use of unit testing is it's kind of like documentation. So we all hate writing documentation and comments.
matter what, we say we never write as many comments as we should. And the nice thing about unit test is it is a concrete example of how the function should work. Yeah. So if somebody doesn't understand how something's supposed to work and you wrote good unit tests, they should be able to look at your unit test and say, oh, he's testing it in this way. Well, why is it? Oh, because it does this. Oh, I see now. And there's
terrible. Yeah. I saw one place where the code was—the comment said, you know, we want like tangent y over x, and the function returned y divided by x. And I couldn't I couldn't get it. I went over to the person. He said, oh yeah, because y and x are very small, and when they're small, the tangent function is equal to the line y equals x for small values. And this is like, you know, at least provide a unit test or some comments or something. If you provide a unit test and your tangent function returns y divided by x, and you have unit tests, you'll be more inclined to think, oh, you know, I should probably write a line a comment here because even to me calling the tangent function and getting the divisor, you know, the ratio doesn't make sense. And so it's sort of as Patrick said, it encourages you to feel like the person on the outside calling the function. If you think about this for people who work on open source or people who are in a job environment, so you're writing your code, then later on you're going to read it—maybe later on is an hour later, a day later, a year later, 10 years later, decade later. You're going to read your that's one person. If you have anybody on your team or anybody on the internet is looking at your project, that's two people. So at minimum twice as many people are going to read your code than write it. You know, so it took one—it took you to write it, but two times you are going to read it minimum, probably more like 50 times or 100 times. That code is going to be written for each time read for each time it was written. And so just, you know, testing is sort of a way for you to just keep thinking about that and putting yourselves in the shoes of the person using
the code. So there are some weaknesses, and I mean we can continue to talk about all this awesome, but some of the weaknesses is that it can get to management. It can seem time-consuming, and even it can get—I mean, even if you're doing a really good job, you're not overdoing the testing. It still can take a lot of time to write unit tests, and especially if you're not writing them as you go, which is really bad. You shouldn't do that, and then you kind of get to the end. Oh, I'm almost done. I just have to write unit tests. Yeah.
Not good. I have to admit I've done a couple of things at my current job where somebody said 'Unit tests?' as a—as a reply. So I'd send all this code out and say, 'Hey, take a look at this cool thing I made,' and let me you know, let me add it to the system. And someone would just reply, 'Unit tests? Turns out there's none.' So yeah, all of us can be guilty of it, and it can feel like like a big problem, but but yeah, just something that you start to live with. Another weakness is you can establish some constraints that might not be valid. So as Patrick was suggesting with his addition example, right? You might have some function that does X. Let's say you have a function that let's say you have some Machine Learning algorithm, right? And you just run your Machine Learning algorithm and you give it a bunch of data, and it spits out 4.3 for a prediction. So you write a unit test where you have this as an input and you output you expect 4.3. That's a very specific number. I mean, if you make any change to your implementation, it'll probably change. It might become 4.2999 or it might become—might become five. And maybe that's okay because the algorithm has changed in some way that's meaningful. So one thing that you have to be really careful about is writing the unit tests in such a way where they test the idea, but in such a way where they don't just—they aren't just very fragile and break because of things that don't really matter. And so this could be really difficult, and sometimes it's impossible. Hard coding numbers leads to this a lot. Yeah. Yeah, definitely.
Yeah, then they change it and it all breaks. Yeah, the other thing is it related to kind of like establishing false constraints. It's just in general you can get a false sense of what the code is supposed to do or how safe it is or how reliable is because oh, it's got unit tests. It's like, yeah, but even if the unit tests cover all the lines of code, even if they cover all the branches of the code, even if there's a lot of them doesn't mean they're testing the right thing or the meaningful thing or that you know crazy cases that you know could cause your system to fail. And then when it fails like, oh, I don't understand what happened. It's unit tested. It's like, well, you know, so you got to be careful about those things because one bad thing about in the way, you know, there's different ways of doing it. So many people have where unit tests are supposed to be written by somebody different than the programmer. And the reason for that is that if you as a programmer write the unit test, you have the same assumptions in your code and the unit test. So for instance, like this tangent function. This guy says, oh, you know, it's fun to take the division, divide, divide the two numbers because for small numbers that's that's how that's okay, right? Well, what if it turns so he writes the unit test to reflect that, but then what if he doesn't have any test that the numbers are sufficiently small and then somebody comes along and uses big numbers or reuses his function, right? So because not just anybody wrote the unit test. He wrote it. He wrote it with the same assumptions. He only tested small numbers. Then that's a false sense like that's a bad thing. Like he wrote it versus if somebody else just said, oh hey, we have this tangent function and that's it. Write a test for it, right? He's gonna write tests for all sorts of numbers, and then that's gonna force the programmer to you know have check to say that this isn't too big of a number, and that makes everybody's life.
So you can write unit tests all on your own, just like you can write mocks all on your own. You can do all this stuff without any outside tools or help, but that's always painful. Yeah, things are made better when you have tools to help you. And so we have a couple here. We're not going to talk about them all. Many, many, many programming languages have choices for what unit tests you want to do, for choices for mocking.
That's the one I've used as well, and I think it's great. Um yeah, you know, I think it's pretty cool. Has a lot of cool settings. If you use Maven, it works well with Maven. Um it'll let you run tests in parallel, which is pretty fun if you have a lot of multi-core machine and things like that. So.
Then you can mock with EasyMock. Yeah, Mockito. Yeah. Uh I'm sure there's more, and in those can have different strengths and weaknesses, and there's different kind of whole different ways of mocking where some you tell it like it's going to call this, then this, then this, and then you call it like a replay, and it's going to replay all the calls you gave it. And others you just say here's what to do, and then you check in the end and say did this happen? Did this happen? Did this happen? So there's kind of different ways of even doing the.
Mocking C++. Yeah, C++ has CPPTest, which is pretty cool. Um and on the mocking side it has GMock, which is a pretty, pretty fun library. And uh C++ because of you know it's it's kind of such a hacky language anyways, the mocks are really hacky and they use all these crazy compiler definitions things like that. But there's plenty of great tutorials mocking in general is.
I mean people in the mocking library you're trying to inject, you know, you're trying you want to keep the class so that the person using the class doesn't know the difference. So you want to keep the signature of the class the same, but at the same time you want to inject all this custom logic, right? And so yeah, that causes craziness to happen. But the good thing is these libraries do the craziness for you and they provide an API that's pretty simple. So if you.
Don't also. Yeah, we mentioned it briefly, but code coverage is saying how many there are different forms of it, but the simplest is statement coverage. So how many of the lines of your code—think about it that way—are executed when you run the unit tests or whatever test? So line five, six, seven, eight, and nine were all run. You got a hundred percent, you know, a hundred percent because the lines before that and after that are comments and curly braces. Great. But then if you have like if statements, you'll have only a hundred percent code coverage if the if statement is evaluated and if the insight is evaluated. The problem is there's this other thing called branch coverage, which says like, oh what happens if different branches are executed? So you could get, for instance, a NullPointerException if you have a badly stated branching, but you only have a hundred percent statement coverage; you wouldn't ever necessarily detect that—that has a NullPointerException, right? Versus if you have branch coverage, you could potentially catch that because branch coverage says every part of the branch has to be evaluated.
Yeah, exactly. Yeah, and so yeah, don't do that yourself. Yeah. But if you go to Java, has a bunch—I actually haven't used any of the open-source ones, so I can't really comment, but I'm sure they're great because most of the stuff, I mean, you think about it, if you write a testing framework, you're already a pretty good programmer. I mean, if you got the Moxie to say, 'Hey world, here's a testing framework everyone should use,' then you probably know what you're doing. So there's definitely good code coverage tools for all the modern languages, and
Then another thing it enables is—and this is a concept I think we've talked about before—but Continuous Integration. So Continuous Integration is that you're every time somebody submits a change into the repository to whatever your source control is, the system a part there's something that runs which rebuilds everything and runs all the tests, right? So every time there's a change, so in many places people just make changes all the time willy-nilly, and then occasionally somebody kind of checks out a version of all the code runs the test against it and sees what—sees what has happened. And Continuous Integration says, 'No, no, no, that's a terrible idea.' Yeah, well, let's you know have a—it's called like a build status, like oh, currently tests are broken or the code won't compile. And then when somebody does something wrong, you can email them, you can notify them, you know, when something's bad. Yeah, and you're just doing this all the time, and when you have unit tests, it helps with that. So if somebody makes a change to your code without updating the unit test or testing it, you know, immediately if something's wrong or broken.
Yeah, and again, don't write this yourself. There's—I think there's Jenkins, and Java, there's a bunch of other ones, but they'll do all sorts of clever things, like they'll run the—they'll run all your tests daily, and then if a test is broken, they'll do a binary search between today and yesterday to like to quickly find out exactly what change broke it. Um so they're pretty awesome, and take advantage.
And I think—I mean, as I was saying before, the some of the like the stress tests that you write when you're unit testing cause you to do things in a different way or learn things about the system that you wouldn't have otherwise known. So, I mean, for example, a really simple example was—uh, I had a system that basically did a bunch of math and it sort of tried to hone in on a certain number. And when I wrote unit tests, I found out that if I gave it a certain set of input, it just the very first unit test just picked a bunch of random numbers and then tried to train it and then saw that the number it converged to—that it actually converged. So, in other words, given a bunch of inputs, the output should start off crazy. It should be, you know, negative 100, then 4,000, then negative 10,000, but it should eventually settle down. So, it might settle on 3 million. I don't know what the final answer is, but it will get to a point where every time you run that function, it would give you some number in really close to 3 million or really close to some X. And I found out that for certain inputs, it just oscillates wildly and it just gives you crazy numbers and it never settled. And it turns out that that algorithm, the fundamental math behind it, was broken. And so, I wouldn't have found that out without unit testing. And so, for that reason, it can actually, you know, it could give you insight into something that you just wouldn't have had otherwise if you had just, you know, ran it out on your input. All right. Well, cool. Till next time. Yeah, have fun writing unit tests. Let us know if you write unit tests first and if you do what how that affects your—that was a cool experience. Tell us how.
Wrong. Yeah, we have the discussion section. People have been posting, so if you have any cool discussion, yeah, Google Plus community—that's right, that the discussions in the G+ community or you can comment on our Plus page. We have the Plus page as well. So there's like there's many ways to contact us. No excuse. There's the blog, the Plus page, is the community. Yes. Oh, also, and you can email us. That's right, you can email programmingthrowdown@gmail.com, and those are all great ways to reach us and let us know how how these things turned out.
