(Fun fact, I looked up capitalization rules for hyphenated words in titles. I was going to capitalize Driven no matter what since it is the first D in TDD, but I thought it would be interesting to know the proper grammar)
I feel like testing in general and test-driven development in particular might be a very polarizing subject for some. Maybe being new to the field helps me have a much more blasé attitude toward the topic. Maybe, for once, my naivety is working in my favor. Or perhaps I am strolling blindly down the biggest primrose path in programming. Only time will tell!
Today I wrote my first ever tests. I even started with a blank slate of a program and made sure that every test fails the first time around. Some of them continue to fail after that first time. Eventually I get them to pass. I’m even up to four tests! It has been an interesting process.
Like everything else in learning to program, the first step is the most daunting. There are so many choices. And, once again, my knowledge and ability isn’t really up to evaluating the choices… at all. So, I turned to Google. Having just purchased WebStorm, I thought it would be a good idea to see what integrates well there. That lead me to some WebStorm documentation (here) that narrowed my choices down. Kind of. It now looked like I needed to pick a test runner AND a unit testing framework. Well, ok. On the list for test runners are:
When I was looking at all this, last night, all of these names were new to me. I dug further into the docs and it looked like Karma had the most extensive write-up, so I chose it. First step to installation was installing node.js, because everything else from here on out would be installed using npm, the node package manager. From there you get some choices. You cna use WebStorm’s GUI to select packages from a list or go straight for the command line. It’s the same process for the frameworks, here are the choices:
I had actually heard of (and did surface research on) both QUnit and Jasmine before, so I thought I would try those out. I spent a while typing away on the command line and getting everything setup and then the moment of truth… how in the world do I actually write my tests? It was pretty late by the time I got all of this accomplished so I decided to get a fresh start in the morning.
What really happened was, I had a long talk with my friend Chris about testing and he even showed me how he got it working in Visual Studio. We went over some tests and the ideas of writing an initially failing test and some of the philosophy behind TDD. In some ways, he is almost as new to testing as I am, so it is interesting to get a grizzled veteran’s take on the whole thing. When that was over, it was really bedtime, and my brain was fried anyway, so off to dreamland I went.
This morning I cleaned out my project and started from fresh. I looked at the WebStorm docs some more and looked at other testing docs, specifically Mocha’s. It actually looked pretty simple to set up, and it turns out I can use it without a test runner, so for the sake of actually getting something done, I gave it a shot. At first, it was pretty hairy. There are still pieces of the puzzle missing for me, so I just copied chunks of the tutorial to get things working. I got tests working to some degree, but I kept (and continue to keep) banging up against the edges of both my comfort zone and my actual knowledge base.
I’m not entirely happy with the details of how I have mocha running, but for now, I am prioritizing having it working over having it working just right. Here is the fruits of my labor:
The tests are on the top and the result on the bottom. Four passing tests! I really have no idea if my tests are too simple or too complex or if I am using the correct syntax, or a million other things that point out my already completely obvious newness to programming. I am learning to get over the nervousness about exposing my ignorance. I will sometimes post stupid example or ask stupid questions, and I have to be ok with that.
In my next post, I will dive more into the details of how I got this all working and the steps I took once I had it working to jump into the TDD way of doing things. As always, I’m always happy to answer questions or explain specific nuances of my process. Thanks for taking the time to read this and, keep coding!