NodeJS: Writing tests -- A Beginners' Guide

26
NodeJS: Writing Tests Docs: http://conancat.github.com/node-test-examples/ Github: https://github.com/conancat/node-test-examples Prepared by Grey Ang @conancat | [email protected] A Beginners’ Guide Wednesday, April 3, 13

description

This is an introductory slide for web development beginners about software testing, and how to write tests in NodeJS. For more information, you can check out the project's page at http://conancat.github.com/node-test-examples/ Get the exercises and test project at this page: https://github.com/conancat/node-test-examples. Need help? Buzz me on Twitter -- @conancat!

Transcript of NodeJS: Writing tests -- A Beginners' Guide

Page 1: NodeJS: Writing tests -- A Beginners' Guide

NodeJS: Writing Tests

Docs: http://conancat.github.com/node-test-examples/Github: https://github.com/conancat/node-test-examples

Prepared by Grey Ang@conancat | [email protected]

A Beginners’ Guide

Wednesday, April 3, 13

Page 2: NodeJS: Writing tests -- A Beginners' Guide

Why write tests?

Wednesday, April 3, 13

Page 3: NodeJS: Writing tests -- A Beginners' Guide

Programming should be fun

Wednesday, April 3, 13

Page 4: NodeJS: Writing tests -- A Beginners' Guide

...and not frustrating.

Wednesday, April 3, 13

Page 5: NodeJS: Writing tests -- A Beginners' Guide

The biggest joy in programming is knowing

what you write does what it intended to do,

and it doesn’t break anything else.

Wednesday, April 3, 13

Page 6: NodeJS: Writing tests -- A Beginners' Guide

We all want a peace of mind. Nirvana.

Wednesday, April 3, 13

Page 7: NodeJS: Writing tests -- A Beginners' Guide

...but we’re all lazy asses. Trust me, I know. I am one too.

Wednesday, April 3, 13

Page 8: NodeJS: Writing tests -- A Beginners' Guide

Most common excuses to not write tests

Laziness: “Writing tests is such a chore!”

Perceived busyness: “We have too many things to do! We have other targets to hit!”

Overconfidence: “This function is really easy. I can handle this.”

Ego: “Come on, real men doesn’t need to test their code. It’ll definitely work. Trust me, I’m a programmer.”

Wednesday, April 3, 13

Page 9: NodeJS: Writing tests -- A Beginners' Guide

What are the consequences?

Wednesday, April 3, 13

Page 10: NodeJS: Writing tests -- A Beginners' Guide

96% of programmers do this to their computers at least once a week.

(Okay, I made up that number. But we all know how that feels.)

Wednesday, April 3, 13

Page 11: NodeJS: Writing tests -- A Beginners' Guide

Old code breaks when you write new code

Code that you wrote is slow as hell

What you’ve done is not what it supposed to do

Your code just broke other people’s code

You spend 8 hours debugging your code and other people’s code

...to name a few.

Shit programmers deal with

Wednesday, April 3, 13

Page 12: NodeJS: Writing tests -- A Beginners' Guide

Whose fault is it? Yours, of course.

Wednesday, April 3, 13

Page 13: NodeJS: Writing tests -- A Beginners' Guide

If you’re a true lazy programmer that doesn’t wanna deal with all that

shit, write tests.

Mark my words. You won’t regret it.

Wednesday, April 3, 13

Page 14: NodeJS: Writing tests -- A Beginners' Guide

TDD & BDDTwo jargons you should know

since yesterday

Wednesday, April 3, 13

Page 15: NodeJS: Writing tests -- A Beginners' Guide

Test Driven Development (TDD)

http://en.wikipedia.org/wiki/Test-driven_development

“...the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards.”

Wednesday, April 3, 13

Page 16: NodeJS: Writing tests -- A Beginners' Guide

Step 1: Write Test

Step 2: Run the test. (It will fail, trust me. It’s okay. Take a deep breath. )

Step 3: Write the MINIMUM AMOUNT code to make the test pass

Step 4: Test performance of code to optimal levels

Step 5: Celebrate that your code works wonderfully

Step 6: Write tests, rinse and repeat.

Test Driven Development (TDD)

Wednesday, April 3, 13

Page 17: NodeJS: Writing tests -- A Beginners' Guide

Problems with traditional TDD

Test cases are written for the code -- not for the manager, client, or the users of the system.

Code works with the test, code passes the test, but code FAIL with user behavior or client requirements.

Traditional TDD is great for development. But if we want code that WORKS, we need to involve the people who spec the app.

Remember, code is written for the benefit of people!

Wednesday, April 3, 13

Page 18: NodeJS: Writing tests -- A Beginners' Guide

Behavior Driven Development

Introducing

It’s just like TDD, really. Only better.

Wednesday, April 3, 13

Page 19: NodeJS: Writing tests -- A Beginners' Guide

Behavior Driven Development (BDD)

http://en.wikipedia.org/wiki/Behavior-driven_development

“...combines the general techniques and principles of TDD with ideas from domain-driven design and object-oriented analysis and design to provide software developers and business analysts with shared tools and a shared process to collaborate on software development.”

Wednesday, April 3, 13

Page 20: NodeJS: Writing tests -- A Beginners' Guide

Step 1: Gather (and argue) requirements from the business side or your users on what should be done

Step 2: Write test cases to meet the pre-defined business requirements

Step 3: Write the MINIMUM AMOUNT code to make the test pass

Step 4: Show it to your business partner

Step 5: ???

Step 6: Profit!

Behavior Driven Development (BDD)

Wednesday, April 3, 13

Page 21: NodeJS: Writing tests -- A Beginners' Guide

“With great power, comes great

responsibility.”-- Uncle Ben, Spiderman

Wednesday, April 3, 13

Page 22: NodeJS: Writing tests -- A Beginners' Guide

You’re Spiderman.

You’re a web developer. You make websites.

Remember, you work with machines AND people.You write code for people.

Wednesday, April 3, 13

Page 24: NodeJS: Writing tests -- A Beginners' Guide

Best way to learn is by doing it yourself.So, lift your lazy ass and go clone this repo and start playing around.

https://github.com/conancat/node-test-examples

Wednesday, April 3, 13

Page 25: NodeJS: Writing tests -- A Beginners' Guide

Need help? SHOUT!Or you can just talk to me at

@conancat on Twitter, or email me at [email protected].

Wednesday, April 3, 13

Page 26: NodeJS: Writing tests -- A Beginners' Guide

Thanks for coming!Be a good Spiderman!

Wednesday, April 3, 13