1、计算机科学概述 Introduction to Computer Science,陆嘉恒 中国人民大学 信息学院,Software Quality and Testing (软件质量与测试),“Software and Cathedrals are much the same: First we build them, then we pray!”-Sam Redwine, Jr.,No matter how well software has been designed and coded, it will inevitably still contain defectsTesting is
2、 the process of executing a program with the intent of finding defectsA “successful” test is one that finds errors, not one that doesnt find errors,“Quality” is Hard to Pin Down (软件质量是很难衡量的),Concise, clear definition is elusive Not easily quantifiable Many things to many people “Youll know it when y
3、ou see it”,Good Quality Software Has,Understandability (可理解的) The ability of a reader of the software to understand its function Critical for maintenance Modifiability (可修改的) The ability of the software to be changed by that reader Almost defines “maintainability“,Good Quality Software Has,Reliabili
4、ty (可靠的) The ability of the software to perform as intended without failure If it isnt reliable, the maintainer must fix it Efficiency (高效的) The ability of the software to operate with minimal use of time and space resources If it isnt efficient, the maintainer must improve it,Good Quality Software
5、Has,Testability (可测试的) The ability of the software to be tested easily Finding/fixing bugs is part of maintenance Enhancements/additions must also be tested Usability (可用的) The ability of the software to be easily used (human factors) Not easily used implies more support calls, enhancements, correct
6、ions,Good Quality Software Has,Portability (可升级的) The ease with which the software can be made useful in another environment Porting is usually done by the maintainer Notice all related to maintenance but these qualities need to be instilled during development,Why Test?,No matter how well software h
7、as been designed and coded, it will inevitably still contain defectsTesting is the process of executing a program with the intent of finding faults (bugs)A “successful” test is one that finds errors, not one that doesnt find errors,Why Test?,Testing can “prove” the presence of faults, but can not “p
8、rove” their absenceBut can increase confidence that a program “works”,What to Test?,Unit test test of small code unit: file, class, individual method or subroutineIntegration test test of several units combined to form a (sub)system, preferably adding one unit at a timeSystem (alpha) test test of a
9、system release by “independent” system testersAcceptance (beta) test test of a release by end-users or their representatives,When to Test?,Early “Agile programming” developers write unit test cases before coding each unit Many software processes involve writing (at least) system/acceptance tests in
10、parallel with developmentOften Regression testing: rerun unit, integration and system/acceptance tests After refactoring Throughout integration Before each release,Defining a Test,Goal the aspect of the system being testedInput specify the actions and conditions that lead up to the test as well as t
11、he input (state of the world, not just parameters) that actually constitutes the testOutcome specify how the system should respond or what it should compute, according to its requirements,Types of software testing,Unit testing Test the basic pieces, e.g. methods Integration testing Combinations of u
12、nits System testing “Black-box” or acceptance testing Regression testing Make sure changes dont break anything,Other types of testing,Obsession testing “Ill fix this bug if its the last thing I do.”Aggression testing “If this doesnt work, Im gonna kill somebody.”Depression testing “If this doesnt wo
13、rk, Im gonna kill myself.”,Unit Testing (单元测试),Unit Testing Overview,Unit testing is testing some program unit in isolation from the rest of the systemUsually the programmer is responsible for testing a unit during its implementationEasier to debug when a test finds a bug (compared to full-system te
14、sting),Unit Testing Strategies,Black box (specification-based) testingWhite box (program-based) testing, aka glass-boxNormally perform both (not alternatives!),White Box Testing (白盒测试),Test suite constructed by inspecting the program (code)Look at specification (requirements, design, etc.) only to d
15、etermine what is an errorAttempt to exercise all statements, all branches, or all paths (control flow and/or data flow)Intuition: If you never tested that part of the code, how can you have any reason to believe that it works?,Whitebox Approaches to Unit Testing,Execute all (reachable) statements Ex
16、ecute all branches of logical decisions, including boundaries of loops Execute all (feasible) control flow paths in combination Execute all data flow paths (from each variable definition to all its uses) Usually applied only to individual subroutines rather than larger unit (due to combinatorics),Bl
17、ack Box Testing,Test suite constructed by inspecting the specification (requirements, design, etc.), not the source codeTests unit against functional and, sometimes, extra-functional specifications (e.g., resource utilization, performance, security)Attempts to force behavior (outcome) that doesnt ma
18、tch specification,Blackbox Approaches to Unit Testing,Functional testing exercise code with valid or nearly valid input for which the expected outcome is known (outcome includes global state and exceptions as well as output)Exhaustive testing usually infeasible, so need way(s) to select test cases a
19、nd determine when “done” testingChoose test cases to attempt to find different faults Equivalence partitioning Boundary value analysis,Automated Testing,Testing by hand is tedious, slow, error-prone and not funComputers are much less easily bored than peopleSo write code to test your code!,Automated
20、 Testing,Write code to set up the unit, call its methods with test inputs, and compare the results to the known correct answersOnce tests are written, they are easy to run, so you are much more likely to run themJUnit is a commonly used tool for testing,Unit Testing Summary,Unit testing is testing some program unit in isolation from the rest of the system Usually the programmer is responsible for testing a unit during its implementationStrategies: Black box (specification-based) testing White box (program-based) testing Normally perform both (not alternatives!),