Download presentation
Presentation is loading. Please wait.
Published byMaria Tyler Modified over 8 years ago
1
Automated Testing with PHPUnit
2
How do you know your code works?
3
Manual Testing Type in a value Submit the form Check by eye
4
Manual Testing Tedious Time Consuming Error Prone Did you test everything? Consistently?
5
Automated Testing Have the computer run your tests
6
Pro: Confidence Know that your code does what it needs to Know that your code doesn’t break anything that used to work Regression Testing
7
Con: Testing isn’t free Writing tests takes time Response: So does manual testing Mo Code Mo Problems Response: It’s a worthwhile investment
8
Unit Testing Automated testing one unit at a time
9
What is a unit? Module (procedural programming) Class (OOP)
10
Breaking Down Unit Tests Test Suite composed of Test Case Classes each of which have Test Methods which make 1 or more Assertions
11
A Unit Test Asserts That A Unit Of Code Performs To A Specification
12
Help XUnit Frameworks
13
XUnit Frameworks Provide Assertions Calls Test Methods Help Setup Test Cases Test Runners
14
Assertions Code to verify that expected and actual values match Often methods starting with “assert…()” or “refute…()” Used by the developer $this->assertTrue() $this-> assertFalse() $this-> assertEquals()
15
Calls Test Methods Knows to call your code which tests a particular condition, method, or other “sub unit” Methods usually begin with “Test…()” Written by the developer class … { public function test…() { … }
16
Help setUp and tearDown Offers special functions that get called before and after each test. Written by the developer class … { public function setUp() {…} public function tearDown() {…} }
17
Unit Test Life Cycle TestCase:: setUp() TestCase:: test*() TestCase:: tearDown()
18
Test Case A class with test methods Usually offers assert methods Extended by the developer class MyTest extends PHPUnit_Framework_ TestCase { … }
19
Provide Test Runners Code that makes executing tests simple Often a command-line tool (useable by IDE)
20
Test First or Test Last?
21
Test First Test Driven Development (TDD)
22
Testing Cycle Write Test Confirm Test Fails Make Test Pass Ensure All Tests Pass
23
Find Problems Early How should your code work? What do the requirements mean?
24
Manage Scope Creep Do The Simplest Thing That Could Possibly Work YAGNI Know when you’re done: All tests are green.
25
Test Last Problematic Tests become biased for the code you already wrote
26
What don’t you test?
27
Don’t Test the Language/Framework Don’t test session session initiaiton Or $_POST/$_GET/etc. Or anything provided by PHP (and/or your framework, $_POST[“loggedIn”] = true; $this-> assertTrue($_POST [“loggedIn”]) # Wrong!
28
Don’t Test Methods Without Logic class … { private $foo; public get_foo() { return $this->foo; }
29
What to Test
30
Boundaries function … { if ($foo > 3) { // test whether execution goes in here } else { // test whether execution goes in here }
31
Invalid values 0 Negative values Empty strings Very long strings (>500chars)
32
What’s a good test? Repeatable Use setUp() to make sure everything is right for your test Independent Use tearDown() to clean up Thorough It’s ok to have more than one test method in your case… in fact it’s often necessary!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.