the way of the web testermedia.pragprog.com/titles/jrtest/unit.pdfthe way of the web tester a...

11
Extracted from: The Way of the Web Tester A Beginner’s Guide to Automating Tests This PDF file contains pages extracted from The Way of the Web Tester, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit http://www.pragprog.com. Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printed versions; the content is otherwise identical. Copyright © 2016 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. The Pragmatic Bookshelf Raleigh, North Carolina

Upload: others

Post on 24-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

Extracted from:

The Way of the Web TesterA Beginner’s Guide to Automating Tests

This PDF file contains pages extracted from The Way of the Web Tester, publishedby the Pragmatic Bookshelf. For more information or to purchase a paperback or

PDF copy, please visit http://www.pragprog.com.

Note: This extract contains some colored text (particularly in code listing). Thisis available only in online versions of the books. The printed versions are blackand white. Pagination might vary between the online and printed versions; the

content is otherwise identical.

Copyright © 2016 The Pragmatic Programmers, LLC.

All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted,in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,

without the prior consent of the publisher.

The Pragmatic BookshelfRaleigh, North Carolina

Page 2: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you
Page 3: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

The Way of the Web TesterA Beginner’s Guide to Automating Tests

Jonathan Rasmusson

The Pragmatic BookshelfRaleigh, North Carolina

Page 4: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

Many of the designations used by manufacturers and sellers to distinguish their productsare claimed as trademarks. Where those designations appear in this book, and The PragmaticProgrammers, LLC was aware of a trademark claim, the designations have been printed ininitial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer,Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade-marks of The Pragmatic Programmers, LLC.

Every precaution was taken in the preparation of this book. However, the publisher assumesno responsibility for errors or omissions, or for damages that may result from the use ofinformation (including program listings) contained herein.

Our Pragmatic books, screencasts, and audio books can help you and your team createbetter software and have more fun. Visit us at https://pragprog.com.

The yellow adhesive note graphic in Chapter 11 is designed by Layerace from Freepik.com.

The team that produced this book includes:

Susannah Davidson Pfalzer (editor)Potomac Indexing, LLC (index)Nicole Abramowitz (copyedit)Gilson Graphics (layout)Janet Furlow (producer)

For sales, volume licensing, and support, please contact [email protected].

For international rights, please contact [email protected].

Copyright © 2016 The Pragmatic Programmers, LLC.All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted,in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,without the prior consent of the publisher.

Printed in the United States of America.ISBN-13: 978-1-68050-183-4Encoded using the finest acid-free high-entropy binary digits.Book version: P1.0—September 2016

Page 5: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

UI

Integration

Unit

In this chapter, we’re going to look at these small little tests developers writecalled unit tests.

While this chapter is primarily focused on developers, it’s a worthwhile readfor testers too. Learning what goes on down at the base of the pyramid willnot only help testers spot potential gaps at the upper levels, it will also givethem great insight into where they should go with their exploratory testing.

Regardless of whether you are a developer or a tester, by the end of thischapter, you will know what unit tests are, how to write them, and why theyform the base of our pyramid.

Everything Is Awesome!Yeah! With our newfound UI testing superpowers, everything is suddenlyawesome! Not only can we write high-level smoke tests, but we can also writeUI tests for practically anything!

Need a smoke test? UI test.

Got a bug? UI test.

Need someone to fill out that pesky weekly timesheet? No problem—UI test(yes, we actually did that).

• Click HERE to purchase this book now. discuss

Page 6: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

UI TestsEvery problem

Yes, to us the world’s problems can now all be solved with one more UI test,and things are going great! Except…

The Challenge with UI TestsHey! Have you noticed that our build times have started to take off?

time

Buildtime

secs

hours

LONGER BUILD TIMES

Huh! That’s strange. What used to take a couple of seconds and minutes nowtakes tens of minutes and hours!

And what’s up with the state of our builds? Why have they all of a suddenstarted to break?

• 6

• Click HERE to purchase this book now. discuss

Page 7: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

X XX XX XXBROKEN BUILDS

Hmmmmm ....

Beats me! But all I know is with our builds taking longer, and the tests con-stantly breaking, we are spending way more time fixing broken tests thanadding new features to our software.

I thought these automated test thingies were supposed to help!

This real-life story of teams simultaneously discovering the magic and painof going with lots of automated UI tests is unfortunately all too common.

It’s not that UI tests are bad. They are not. They are just not made for thetwo things we crave above all else when doing rapid iterative development:feedback and speed.

1ms100ms

Integration Unit UI

1000ms

Tend to be slowYou see, UI tests are slow. Really slow. What takes milliseconds in a unit testcan take seconds in a UI test. And while that may not sound like a long time,once you start to get a lot of these longer running tests, the cumulative timecan really start to add up.

Not only are UI tests slow, they have a reputation for being flaky and fragile.

• Click HERE to purchase this book now. discuss

The Challenge with UI Tests • 7

Page 8: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

fill_in 'Name'

fill_in 'Address'

click_button 'Register'

Can be fragile

X

Flaky means they don’t always run reliably—sometimes they pass, sometimesthey fail (we will talk more about why later). But more than that, because UItests are so closely tied to the user interface, the smallest change in function-ality can end up breaking a UI test, even though it looks like it had nothingto do with it.

Finally, while UI tests are great at telling you that something’s wrong, theyare lousy at telling you where the problem is.

Integration?UI? Unit?

Aren’t very precise

I know there’s a bug in here somewhere ...

Hehe

Remember—these tests go end-to-end. So finding and fixing a bug can be alot like searching for a needle in a haystack.

Nope. As good and as cool as UI tests are, they alone are not enough. Whatwe need is another kind of test. Something that’s:

• Fast• Cheap• Precise• Gives us rapid feedback

• 8

• Click HERE to purchase this book now. discuss

Page 9: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

Enter the Unit TestUnit tests are small, method-level tests developers write to prove to themselvestheir software works.

For example, say you were writing a program that could play blackjack, andyou wanted to verify that all newly shuffled decks contained fifty-two cards.You could write a unit test for that. Something like this:

def test_full_deckfull_deck = Dealer.full_deckassert_equal(52, full_deck.count)

end

Unlike UI and integration tests, unit tests are small and fast. They don’t goend-to-end through all the layers of a system. They tend to be more local.And it’s this smallness that makes them fast, focused, and easy to work with.You can write a unit test for just about anything—like testing assumptions.

52 cardsNo Joker!

AKQJ1098765432 13 hearts, spades, diamonds, clubs

Great for testing assumptionsAssumptions get us all the time in software. Not anymore. With automatedunit tests, hidden assumptions can now be tested and verified, along withbusiness logic.

• Click HERE to purchase this book now. discuss

Enter the Unit Test • 9

Page 10: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

Boom!

Calculations and business logicBusiness logic can be complex. The rules we apply easily as humans all needto be codified and somehow tested in the software. What better way to verifywe got the rules right than to code them up in the form of automated tests?

And when it comes to edge cases, unit tests have our backs there too.

if (salary < 100000) taxRate = 30%else if (salary < 70000) taxRate = 20%

99,999?100,000?

69,999?70,000?

Edge cases and boundary conditionsEvery time you can think of a new edge case, off-by-one error, or an error inlogic, you can write a unit test to confirm these things are working as expected.

For these reasons, unit tests have become an indispensable tool for writingsoftware today. This is why now every modern programming language has them.

101011

Business logic

Program flow

Assumptions

Off-by-one errors

Edge cases

Permutations

Unit testsfor all your computing needs

Instant feedback!

100% Satisfaction! Guaranteed!

• 10

• Click HERE to purchase this book now. discuss

Page 11: The Way of the Web Testermedia.pragprog.com/titles/jrtest/unit.pdfThe Way of the Web Tester A Beginner’s Guide to Automating Tests Jonathan Rasmusson ... Regardless of whether you

OK. So that’s what unit tests are. Let’s now dig a little bit deeper and see howthese things work.

• Click HERE to purchase this book now. discuss

Enter the Unit Test • 11