Download presentation
Presentation is loading. Please wait.
Published byDamian Francis Modified over 8 years ago
1
CEN 5070 – Software V&V Automation for Unit Testing © 2001-2010, E.L. Jones
2
6/2004Unit Test Automation2 Purpose This lecture introduces design patterns to automate the testing of program components such as procedures and classes. The testing lifecycle phases involved are implementation, execution and evaluation.
3
6/2004Unit Test Automation3 Agenda Testing Callable Modules Putting it All Together Module Test Drivers Complex Test Drivers Database Testing
4
6/2004Unit Test Automation4 Testing Modules -- Overview Directly testing callable modules requires isolating the module and capturing the context in which it is called. Because a module is not a free-standing executable program, additional effort is required for this "code level" testing, but certain aspects can be automated, providing repeatability, efficiency and documentation.
5
6/2004Unit Test Automation5 Agenda Testing Callable Modules Putting it All Together Module Test Drivers Complex Test Drivers Database Testing
6
6/2004Unit Test Automation6 Testing Modules -- Drivers A test driver executes a unit with test case data and captures the results. Driver Test Cases External Effects Unit Arguments Results Test Results
7
6/2004Unit Test Automation7 Implementing Test Drivers Complexity –Arguments/Results only –Special set-up required to execute unit –External effects capture/inquiry –Oracle announcing "PASS"/"FAIL" Major Benefits –Automated, repeatable test script –Documented evidence of testing –A universal design pattern
8
6/2004Unit Test Automation8 Test Driver for Unit Pay Driver D_pay uses unit_environment E; { declare Hrs, Rate, expected; testcase_no = 0; open tci_file("tci_pay.txt"); open utr_file("utr_pay.txt"); while (more data in tci_file) { read(tci_file, Hrs, Rate); read(tci_file, expected); testresult = pay(Hrs, Rate); write (utr_file, testcase_no++, Hrs, Rate, expected, testresult); }//while close tci_file, utr_file; }//driver
9
6/2004Unit Test Automation9 Test Driver Files (Pay) Test Data File File name: tci_pay.txt Format: (test cases only) rate hours expected-pay File content: 10 40 400 10 50 550 10 0 0 ------ Note: No environment setup. Test Results File File name: utr_pay.txt Format: case# rate hours exp-pay act-pay File content: 1 10 40 400 400 2 10 50 550 500 3 10 0 0 0 ------ Note: Results file must be inspected for failures. Pass Fail Pass Test Script!!
10
6/2004Unit Test Automation10 Agenda Testing Callable Modules Putting it All Together Module Test Drivers Complex Test Drivers - Environment Database Testing
11
6/2004Unit Test Automation11 Testing Modules -- Drivers with Test Environment Set-Up The driver initializes the execution environment of the unit, and examines it after testing completes. External Effects Unit Driver Test Cases Arguments Results Test Results Environment Execution Environment Setup / Inquire
12
6/2004Unit Test Automation12 Test Driver Files (Pay2) (Accumulates grand total of pay amounts) Pass Fail Pass Fail Test Data File File name: tci_pay.txt Format: initial_pay_total final_pay_total {rate hours expected-pay} File content: 1000 1950 10 40 400 10 50 550 10 0 0 ------ Note: Environment setup = initialize global variable. Test Results File File name: utr_pay.txt Format: case# rate hours exp-pay act-pay env_init, env_exp, env_final File content: 1 10 40 400 400 2 10 50 550 500 3 10 0 0 0 1000 1950 1900 ------ Note: Results file must be inspected for failures. Test Script!! Initial / Expected Grand Total
13
6/2004Unit Test Automation13 Test Driver for Pay2 (Accumulates grand total of pay amounts) Driver D global total_pay; { open tci_file ("tci_pay2.txt"); open utr_file("utr_pay2.txt"); //Set up unit_environment E read (tci_file, initial_tot, exp_tot); total_pay = initial_tot; testcase_no = 0; while (more data in tci_file) { read (tci_file, Hrs, Rate); read (tci_file, expected); testresult = pay2(Hrs, Rate); write (utr_file, testcase_no++, Hrs, Rate, expected, testresult); }//while final_tot = total_pay; write(utr_file, initial_tot, exp_tot, final_tot); close tci_file, utr_file; }//driver
14
6/2004Unit Test Automation14 Unit Test Driver Design -- Test Environment Set-Up/Inquiry Driver D uses unit_environment E; { open tci_file (filename); open utr_file (filename); //Set up unit_environment E read (tci_file, E_initial, E_expect); E.set (E_initial); testcase_no = 0; while (more data in tci_file) { read (tci_file, testdata); read (tci_file, expected); testresult = unit(testdata); write (utr_file, testcase_no++, testdata, expected, testresult); }//while E.get(E_final); write (utr_file, E_initial, E_expect, E_final); close tci_file, utr_file; }//driver
15
6/2004Unit Test Automation15 Your Turn -- Test Driver Design Unit GenPayroll(PayDate) computes pay from all hourly employee time records (in file "time.dat") for the previous week. Employee IDs, pay rates, and YTD earnings are stored in the Employee database ("emp.db"). GenPayroll calls unit Pay (Hours, Rate) to compute gross pay. Next, GenPayroll deducts a flat 10%, then writes PayDate, EmployeeID, Hours, Rate, GrossPay, Deductions, and NetPay to the Payroll Register (file "payreg.dat"). Finally, GenPayroll updates YTD-Earning for the employee. ------------------------------- Design the test driver for GenPayroll. Focus on (1) required set-up; (2) data preparation; (3) test execution; (4) verification of results.
16
6/2004Unit Test Automation16 Your Turn -- Test Driver Design Driver PayDate GenPayroll Pay Rate Hours GrossPay file "time.dat" database "emp.db" file "payreg.dat" Verification Set-Up
17
6/2004Unit Test Automation17 Agenda Testing Callable Modules Putting it All Together Module Test Drivers Complex Test Drivers Database Testing
18
6/2004Unit Test Automation18 Testing DB Procedures -- Overview Since the database plays a central role in a system, database procedures should be tested carefully. Automating this testing permits thorough testing and efficient verification via database queries. Trusted database code is a candidate for reuse.
19
6/2004Unit Test Automation19 Testing DB Procedures -- Process Set-Up Initialize DB to baseline (or error) state Run Test Script (manual/automated) Run Verification Script (manual/auto) Verify queries, updates A test driver can combine the test and verification scripts into a single, comprehensive test.
20
6/2004Unit Test Automation20 Testing Callable Units -- Summary A test driver is a reusable design pattern. Test drivers give code-level visibility into the behavior of units, while documenting the testing process. A unit driver can be used for indirect testing of lower level modules. The unit test driver can be extended to test complex units (classes).
21
6/2004Unit Test Automation21 Agenda Testing Callable Modules Putting it All Together Module Test Drivers Complex Test Drivers Database Testing
22
6/2004Unit Test Automation22 Putting It Together Test design is the central thing Test scripts define the sequence of test actions -- a repeatable process Test drivers automate test scripts and produce test documentation Automation is reusable!
23
6/2004Unit Test Automation23 CONCLUSION Unit testing lays the foundation for software testing. The techniques covered in this class extend from simple to complex units and, as we will see, to integration testing. Examples and templates facilitate the process while guiding the tester through a systematic design process.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.