An Introduction To Software Development - Test Driven Development, Part 1

25
An Introduction To Software Development Using Python Spring Semester, 2014 Class #13: Test Driven Development, Part 1

Transcript of An Introduction To Software Development - Test Driven Development, Part 1

An Introduction To Software Development

Using Python

Spring Semester, 2014

Class #13:Test Driven Development, Part 1

3 Different Types Of Testing

• Unit Testing– Performed by developers– Goal is to ensure that their code works correctly

• System Testing– Performed by professional testers– Goal is to ensure that the parts work together

• User Testing– Performed by professional testers– Goal is to ensure that the expected functions work

Image Credit www.fotosearch.com

Python Has Two Testing Tools

unittest• Automated testing framework

• Python’s unittest module, sometimes referred to as PyUnit, is based on the XUnit framework design by Kent Beck and Erich Gamma.

• The same pattern is repeated in many other languages, including C, perl, Java, and Smalltalk.

• The framework implemented by unittest supports fixtures, test suites, and a test runner to enable automated testing for your code.

Py.test• A mature full-featured Python testing

tool

• Provides easy no-boilerplate testing

• Scales from simple unit to complex functional testing

• Integrates with other testing methods and tools:

• Extensive plugin and customization system:

What Would A Py.Test Script Look Like For

Homework #1?## Encrypt the social security numberencryptedSS = "" encryptedSS += socialSecurityNum[10]encryptedSS += socialSecurityNum[9]encryptedSS += socialSecurityNum[8]encryptedSS += socialSecurityNum[7]encryptedSS += socialSecurityNum[6]encryptedSS += socialSecurityNum[5]encryptedSS += socialSecurityNum[4]encryptedSS += socialSecurityNum[3]encryptedSS += socialSecurityNum[2]encryptedSS += socialSecurityNum[1]encryptedSS += socialSecurityNum[0]

def test_encrypted_SS (): assert encryptedSS == ‘6270-55-461’

Image Credit www.objective.no

What Is Continuous Integration?

• Version control keeps track of our code• Now we have automated tests• We need to tie these two things together• Continuous Integration tools do the following:

– Compile code– Run the automated tests– Display and mail out reports when new code is committed

to the repository

Image Credit www.guruintechnocrats.com

Continuous Integration In Action

Bob

1. Bob checks code intothe server

2. Version control codenotifies continuous integration tool that thereis new code

3. Continuous integration toolchecks out the new code, compilesit and runs all of your tests on it.Most build tools also create aweb page and email the team tolet them know how things are going.

How Many Tests Do You Need?

• Trade off: how much of the code that you test vs. the chances of finding a bug in the part that you haven’t tested.

• Testing the same code several different ways won’t do you much good.

• Don’t focus on the number of tests you have, instead focus on the coverage of your tests: % of code tested.

• Most projects aim for 85% - 90% coverage

Image Credit www.clker.com

What If…?

• Good software needs to work.

• How do you know that your software works?

• Even with unit testing, there is the possibility that a portion of your code is untested.

• What if testing was a fundamental part of your software development process?

• What if everything was done with testing in mind?

• Version Control + Continuous Integration + Automated Testing

New Idea: Test First, Not Last!

• Don’t try to go back and work testing into a completed project.

• Instead, build support for testing into the project from the start.

• New idea: Test Driven Design (TDD) – create code with testing in mind from the start.

Pay with Visa

Pay with MC

Pay with Paypal

33

5

Pay with Visa / MC / Paypal

Test First…

• The “Pay With Visa / Mastercard / PayPal” user story is going to be broken into tasks.

• If we are going to test first, then we need to look at our first task

• If we jump right into creating code, then we’ll be right back where we’ve been doing testing last.

Pay with Visa

Pay with MC

Pay with Paypal

33

5

Pay with Visa / MC / Paypal

Analyze The Task

• First we have to break this task down. For this task we’ll have to:

– Represent the Order Information: We’ll have to capture the customer’s name, what they are ordering, and the cost

– Represent the Credit Card Information: We’ll need the credit card info, and their secret card number.

– Represent Receipt Information: We’ll have to capture the confirmation number, the final cost, as well as the date of the transaction.

Pay with Visa

3

Create The Test First!

• Write a test case first!

• Start with the order information part of the task.

• Use your test framework to create a test for the “Pay with Visa” functionality.

Welcome To TDD!

• When you are creating test cases before you write code and then letting those test cases drive how you create your code, you are using Test Driven Development (TDD).

• TDD is a formal term that is used to describe the process of testing from the outset of development.

• This means that you write every line of code specifically as a response to your tests.

How To Write A Test Case

• Your first step needs to be to determine just exactly what needs to be tested.

• Since this is fine grained testing, testing at the unit testing level, you should start with a small test.

• Determine what the smallest test that you could write would be that uses the order information that you’ll be storing as a part of the first task?

Test Case Creation Secrets

• You have no code! You are writing your tests first.

• There is no way that this test should pass the first time that you run it.

• The test probably won’t even compile. However, that’s ok…

• Remember, at first your test case…fails miserably.

TDD Rule #1

• TDD Rule #1: Your test should always fail before you implement any code.

• You want your tests to fail when you first write them.

• The point of the test is to establish a measurable success.

• Because your test is failing, now it’s clear what you have to do to make sure that test passes.

Your Next Step…

• Write the simplest code just to get this test to pass.

• This is called “… getting your tests to green.”• Green refers to a green bar that many

automated test case runners display when all tests pass. If any test fails, a red bar is displayed.

• TDD Rule #2: Implement the simplest code possible to make your test cases pass.

The YAGNI Principal

• Test-Driven Development is about doing the simplest thing that you can do in order to get your test case to pass.

• Do not add anything that you MIGHT NEED in the future.

• If you do need something in the future, you’ll write a test case for it and then you’ll write code to pass that test case.

• Focusing on small bits of code is the key to test-driven development.

• YAGNI: “Ya ain’t going to need it”

3 Steps To Test Driven Development

• Red: Your Test Fails– Write a test to check whatever functionality you are going to write.– It will fail because you have not yet implemented that functionality.– This is the red stage because your testing GUI will show the test in red (failing).

• Green: Your Test Passes– Implement the functionality to get that test to pass– Write the simplest code possible to get the test to pass. – This is the green stage.

• Refactor: Clean up – duplication, ugly code, old code, etc.– After your test passes, go back in and clean up things that you noticed while

implementing your code.– This is the refactor stage.– Next, go on to create the next test.

Pay with Visa

3

Pay with Visa / MC / Paypal

Exercise: Pay With Visa

• Represent the Order Information: We’ll have to capture the customer’s name, what they are ordering, and the cost

– What tests can we create for the order information task?

– What will be the minimal amount of code that we can implement to pass these tests?

Image Credit: presstigeprinting.com

In TDD, Tests Drive Your Implementation

• TDD drives your implementation all the way through development.

• By writing tests before code, you have to focus on the functionality right off the bat.

• What is the code that you are creating supposed to do?

Good TDD Habits

• Each test should verify only one thing– Make each test only test one thing

• Avoid duplicate test code– Just like you avoid duplicate code, avoid duplicate tests.

• Keep your tests in a mirror directory of your source code– You will be creating a lot of test cases– Keep your tests in a separate subdirectory on the same level as your

source code and with the same directory structure. – This will make life easier for your build scripts

Completing A Task

• You’ve got all the tests that you need and they all pass.

• When your tests pass, move on!• Different task, use the same process…

What We Covered Today

1. Test first, not last!

2. Test-Driven Development

3. TDD Rule #1: Your test should always fail before you implement any code.

4. TDD Rule #2: Implement the simplest code possible to make your test cases pass.

Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/

What We’ll Be Covering Next Time

1. Test driven development, Part 2

Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/