One of the best features in Djangois the test client that comes with the test framework -> blackbox testing baby, yeah!
Blackbox testing is my favourite kind of testing. Much more useful than unit tests and generally a lot easier to write, maintain and deal with. And that's because rather than making sure all your functions and deep internals are behaving like they should, you only test what really matters in a web app.
Does the right output happen for this specific input?
Your users don't care that you have full unit test coverage. They will never see the output of all your internal functions and methods and database drivers and whatnot. All they care is that if they put in X they get the correct Y out.
The two main benefits of blackbox testing for a developer are:
- Low maintenance tests - you don't have to rewrite the whole test suite when refactoring the deep internals of your app
- Integration tests - since you're only matching input to output you are testing your whole chain, everything from routing, to database connections and the business logic of the app itself
Although it's still prudent to write enough of these tests to check for correctly returning errors, making sure your app doesn't crash on strange inputs and so on.
It's also important not to forget unit tests for core business logic. That's algorithmic stuff, algorithms should have unit tests attached to them. But in general, web apps aren't algorithms, they're systems. And systems need blackbox tests.
Doing it in node.js
Ok so blackbox testing is an awesome fit for web apps. But how do you do them?
In django this is simple, when in a test you just say something like
response = self.client.get("/my/url")
self.assertEqual(response.status, 200)
and check the response is what you expected. The test client automagically ensures there is a fake server running, that it takes requests, responds and is then torn down when you're done testing.
Until recently the only way I knew of doing this with node.js has been to run the server and send it requests. But that's a bit lame, after all, I want to just run the tests. Of course I'm going to forget to run the server every single time.
Not to mention you have to keep restarting it when you change the code.
Enter Supertest, stage left.
Supertest is a high-level http test library based on superagent, which in turn is a high-level abstraction thingy for doing http requests.
It takes any type of server object and lets you run requests and check for expected results in a very simple manner. If need be, you can even drop down to the low-level API's for http requests.
A simple (mocha) test might look something like this:
var app = require("../app.js");
describe("GET /users", function () {
it("respond with json", function (done) {
request(app)
.get("/user")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200, done);
});
});
Where app is, for example, an express.js server - request is our test client. We are just checking that sending a GET request to /user will return some sort of json and that the status code will be 200.
If the server isn't running when doing that require then supertest will make sure it is bound to an ephemereal port and then brought back down after the tests are done.
Oh and because of superagent you can send any type of request and check for any type of response.
Awesome.
Continue reading about Blackbox testing node.js apps
Semantically similar articles hand-picked by GPT-4
- Testing Backbone apps with Mocha
- Simple trick that lets you code twice as fast
- Testing socket.io apps
- Unit testing is for lazy people
- How to run JavaScript tests in Chrome on Travis
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️