Professional Documents
Culture Documents
4 Chapter04 Software Testing
4 Chapter04 Software Testing
=> Kiểm thử luôn gắn kèm với gỡ rỗi. Kiểm thử thành công ~ phần mềm vận hành đúng
như mong đợi.
4.1 Kiểm thử - giới thiệu
Mô hình chữ V
Kiểm thử đơn vị →
kiểm thử chấp thuận
a. Kiểm thử đơn vị - Unit Testing
~ Áp dụng trên các mô đun riêng lẻ (các hàm, lớp, thủ tục, phương thức, ...)
Thực hiện bởi Lập trình viên (không phải kiểm thử viên)
Có thể áp dụng kỹ thuật kiểm thử hộp đen và hộp trắng.
b. Kiểm thử tích hợp - Integration Testing
Mục đích:
Tích hợp các thành phần &
Phát hiện lỗi giao tiếp xảy ra giữa các thành phần trong/sau khi tích hợp
Các chiến lược kiểm thử tích hợp?
b. Kiểm thử tích hợp - Integration Testing
Mục đích:
Đánh giá phần mềm (sau khi tích hợp/đóng gói)
Các hình thức test hệ thống:
1. Kiểm thử chức năng (Functional Test)
2. Kiểm thử hiệu năng (Performance Test)
3. Kiểm thử sức căng (stress testing)
4. Kiểm thử khả năng phục hồi (Recovery Test)
5. Kiểm thử quá tải (overload testing)
6. ...
d. Kiểm thử chấp nhận - Acceptance Testing
Mục tiêu:
Chứng minh phần mềm thỏa mãn tất cả yêu cầu của khách hàng và khách hàng chấp
nhận sản phẩm.
Khách hàng thực hiện (hoặc ủy quyền cho bên thứ ba thực hiện – vd: QA).
d. Kiểm thử chấp nhận - Acceptance Testing
Line coverage:
Measure the percentage of program code lines indeed executed
A full line coverage: requires every line of code be executed at least once during the process
of testing (~ basic path testing of a program)
Example: Line & path coverage
Xét Mô đun tính giá vé cho dịch vụ taxi nội thành: ITS - The Imperial Taxi Services taximeter
Example – ITS
ITS: Serves one-time passengers & regular clients (identified by a taxi card).
For one-time passengers are calculated as follows:
1. Minimal fare: $2. This fare covers the distance traveled up to 1000 yards and waiting time
(stopping for traffic lights or traffic jams, etc.) of up to 3 minutes.
2. For every additional 250 yards or part of it: 25 cents.
3. For every additional 2 minutes of stopping or waiting or part thereof: 20 cents.
4. One suitcase: 0 change; each additional suitcase: $1.
5. Night supplement: 25%, effective for journeys between 21.00 and 06.00 Regular clients
are entitled to a 10% discount and are not charged the night supplement.
Example – ITS
WT > 3 WT ≤ 3
5
6 Waiting time 7
S >1 8 S≤1
9 No.of suitcases 10
Yes 11 No
12 Regular 13
client?
14
Night
Yes journey? No
15
16
17
Print receipt.
Example – the Imperial Taxi Services (ITS)
taximeter
Test cases required by full line coverage (basic path testing) versus full path
coverage is 3:24 = 1:8!
This ratio grows rapidly with program complexity.
McCabe’s cyclomatic /chu kỳ complexity metrics: Support for the basic path testing
strategy by:
Providing the software complexity metrics &
Giving an upper limit to the number of test cases needed for full line coverage.
=> McCabe’s cyclomatic complexity metrics?
McCabe’s cyclomatic complexity metrics
1) Tính (V(G))=?
Ví dụ: Xét mô đun tính giá vé taxi (ITS):
1
ta có:
R=6; E=21; N=17; P=5 2
R1 4
Þ Thay vào công tính tính V(G): 3
5
(1) V(G) = R = 6
R2 7
6
(2) V(G) = E – N + 2 = 21 – 17 + 2 = 6
8
(3) V(G) = P + 1 = 5 + 1 = 6 R6
9 R3 10
11
=> maximal set of six independent paths is
12 13
shown in Table 9.6.
R4 14
15
R5 16
17
McCabe’s cyclomatic complexity metrics
1
2
R1 4
3
5
R2 7
6
8
R6
9 R3 10
11
12 13
R4 14
15
R5 16
17
=> V(G) - The upper limit to the number of test cases needed for
full line coverage
McCabe’s cyclomatic complexity metrics
SV thực hành:
1. Measures the complexity (the quality &testability characteristics) of a program or
module apply McCabe’s cyclomatic complexity metrics
2. Sử dụng công cụ để đánh giá độ bao phủ dòng code (line coverage) của các test case
đã thiết kế
Ví dụ: Sử dụng JUnit để test đơn vị & một số công cụ tìm code bugs, đánh giá độ báo
phủ code &tính hiệu quả của các test case đã thiết kế
See (below)
4.4.1 Kiểm thử hộp trắng – Examples
Các ví dụ:
a. Unit Test: Maven + Junit:
b. Đánh giá độ bao phủ code (Code Coverage) và tính hiệu quả
(effectiveness) của các test case
Maven – JaCoCo code coverage example
Maven – PITest mutation testing example
c. Phân tích mã nguồn tĩnh (Static Code Analysis) để tìm code bugs
Maven – SpotBugs example
Maven – PMD example
a. Unit Test: Maven + Junit:
Example: xét dự án Java: maven-unit-test
Run:
$ git clone https://github.com/mkyong/maven-examples.git
$ cd maven-examples/maven-unit-test
$ mvn test
4.4.1 Kiểm thử hộp trắng – Examples
Step 3. Run:
cd maven-code-coverage
mvn clean test
=> JaCoCo code coverage report will be generated at target/site/jacoco/*
Open target/site/jacoco/index.html file, to see results:
Maven – JaCoCo code coverage
Results:
• Green – Code is tested or covered.
• Red – Code is not tested or covered.
• Yellow – Code is partially tested or covered.
Maven – JaCoCo code coverage
PITest mutation
<artifactId>pitest-maven</artifactId>
<version>1.4.3</version>
testing <executions>
<execution>
<id>pit-report</id>
<!-- optional, this example attached the goal into mvn test phase -->
<phase>test</phase>
<goals>
To enable PIT mutation <goal>mutationCoverage</goal>
</goals>
testing, put pitest-maven in </execution>
POM file: </executions>
By default, PITest will use different mutators to transform the above code into different mutations (new
code) :
See (below):
#1 Mutation – Changed conditional boundary (mutator)
Code after
public boolean isPositive(int number) { changing
boolean result = false;
if (number > 0) {// mutator - changed conditional boundary
result = true;
public boolean isPositive(int number) {
}
boolean result = false;
return result; if (number >= 0) {
} result = true;
}
return result;
#2 Mutation – Negated conditional (mutator) }
@Test
public void testPositive() {
CalculatorService obj = new CalculatorService();
assertEquals(true, obj.isPositive(10));
}
=> This unit test will kill the mutation #2 and #3 (unit test is failed), but the mutation #1 is
survived (unit test is passed)
Maven – PITest mutation testing
@Test
public void testPositive() {
CalculatorService obj = new CalculatorService();
assertEquals(true, obj.isPositive(10));
//kill mutation #1
assertEquals(true, obj.isPositive(0));
}
b. Code Coverage
Maven – JaCoCo code coverage example
Maven – PITest mutation testing example
SpotBugs (for Java): can detect >400 bug patterns (Java, Spotbugs tool)
=> See link:
https://spotbugs.readthedocs.io/en/latest/bugDescriptions.html
Example:
A simple Java code with:
An unused field ‘abc’ &
A performance issue in the “+ string” loop.
Þ SpotBugs will detect it & showing it on the report
Þ See Java code:
Fig (below):
package com.mkyong.examples;
System.out.println(ip);
}
}
Maven – SpotBugs
b. Code Coverage
Maven – JaCoCo code coverage example
Maven – PITest mutation testing example
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.11.0</version>
</plugin>
</plugins>
</reporting>
Maven – PMD example
Steps:
Step 2: Run: mvn compile site
Þ The PMD report will be generated and integrated into the Maven site automatically
Review the report at target/site/pmd.html
Example: java code (above)
Example:
Black box of testing the output
correctness using Equivalence
classes partitioning (EC)?
Equivalence classes partitioning (EC) for
output correctness tests
EC:
is based on software specification documentation, not on the code.
A Equivalence class: set of input variable values that produce the same output results
An EC that contains only valid states is defined as a "valid EC," whereas an EC that contains only
invalid states is defined as the "invalid EC."
In cases where a program's input is provided by several variables, valid and invalid ECs should be
defined for each variable.
Equivalence classes for output correctness
tests
Example 1:
Equivalence classes (EC) for output
correctness tests
Example 2 – the Golden Splash Swimming Center
Aim: calculates entrance ticket prices for the Golden Splash Swimming Center. The
Center’s ticket price depends on four variables:
1) day (weekday, weekend),
2) visitor’s status (OT = one time, M = member),
3) entry hour (6.00–19.00, 19.01–24.00), and
4) visitor’s age (up to 16, 16.01–60, 60.01–120).
Þ The entrance ticket price table is shown in Table 9.7 (below).
Þ The equivalence classes and the corresponding test case values (EC: weak) are
presented in Tables 9.8 and 9.9.
Þ A total of 15 test cases cover all the defined ECs.
Þ EC: is applied mainly with correctness tests, but it may be used for testing revision & transition factors.
Nội dung chính
từ FPT
4.5 Quá trình kiểm thử
1. Lập kế hoạch test: cần 2. Chuẩn bị test: 4. Báo cáo và phân tích
x.định rõ: Tìm hiểu nghiệp vụ của hệ thống dữ liệu kiểm thử:
phải kiểm thử.
Các giai đoạn kiểm thử áp Lập báo cáo kiểm thử.
Xây dựng kịch bản kiểm thử.
dụng cho dự án phần mềm. Phân tích nguyên
Chuẩn bị dữ liệu kiểm thử. nhân và đề xuất các
Các phương pháp kiểm thử. hành động khắc phục.
Xem xét phê duyệt các tài liệu kiểm
Các công cụ kiểm thử. thử.
Nguồn lực kiểm thử. 3. Thực thi test:
Môi trường kiểm thử bao Thực hiện kiểm thử dựa trên các
gồm tài nguyên phần cứng kịch bản kiểm thử, test case, thủ tục,
và phần mềm. dữ liệu có sẵn từ bước chuẩn bị kiểm
thử.
Mốc bàn giao các tài liệu Thực hiện quá trình quản lí lỗi: báo
kiểm thử. lỗi, sửa lỗi.
4.5 Tiến trình kiểm thử
Test planning M M
Test design M M
Preparing test cases M M
Performance of the tests A M
Preparing the test log and test reports A M
Regression tests A M
Preparing the tests log & test reports M M
Test planning M M
Test design A M
Các phần mềm phục vụ kiểm thử là các phần mềm được sử dụng trong suốt quá
trình kiểm thử:
Từ giai đoạn lên kế hoạch kiểm thử → giai đoạn tổng hợp báo cáo kết quả kiểm thử.
Dựa vào mục đích sử dụng → phần mềm phục vụ kiểm thử chia thành 3 nhóm chính:
1. Phần mềm hỗ trợ viết tài liệu:
2. Phần mềm quản lý lỗi
3. Công cụ kiểm thử tự động.
i. Phần mềm hỗ trợ viết tài liệu
CSDL lưu trữ các thông tin về lỗi được phát hiện như:
Thời gian phát hiện lỗi
Mức độ nghiêm trọng của lỗi
Các khắc phục lỗi, ...
Ví dụ:
Bugzilla (http://www.bugzilla.org/)
Redmine (phần mềm nguồn mở, http://www.redmine.org/),
Atlassian JIRA (http://www.atlassian.com)
...
iii. Công cụ kiểm thử tự động
Mỗi test tool hỗ trợ một khía cạnh test riêng → Phân loại theo mục đích sử dụng
gồm:
1) Unit Testing Tools
2) Regression Testing Tools,
3) Functional Testing Tools,
4) Performance Testing Tools,
5) …
1) Unit Testing Tools
JUnit.
Jtest
Kiểm thử ứng dụng Java:
kiểm tra mức đơn vị, phân tích tĩnh, kiểm tra hồi quy, phát hiện lỗi thời gian chạy, và xem
xét mã, ...
link: http://www.parasoft.com
Sonar:
Nền tảng quản lý chất lượng nguồn mở Java, dành riêng cho liên tục phân tích và đo
lường chất lượng mã nguồn
link: http://www.sonatype.com/
...
2) Regression Testing Tools
Selenium IDE.
SilkTest:
Link: http://www.borland.com/products/silktest/
QA WIZARD:
Áp dụng các ứng dụng Web và các ứng dụng Windows
Link: http://www.qawizard.com/
...
3) Functional Testing Tools
Selenium HQ.
ActiWATE:
Mô phỏng hành động dựa trên Brower, tạo ra các kịch bản dựa trên hành động / kết
quả.
Hỗ trợ HTTP, HTTPS & Ajax.
....
4) Performance Testing Tools
Hỗ trợ:
Load testing; Volume testing, Stress testing, performance testing, reliability testing
Các công cụ:
Loadrunner;
Pylot,
Apache JMeter,
...
5) Các nhóm test tools tự động khác