ソフトウェアテスト
[11] テスト駆動開発演習
Software Testing
[11] Test-driven development Exercise
あまん ひろひさ ひろひさ
阿萬 裕久 裕久( AMAN
Hirohisa )
(C) 2007-2022 Hirohisa AMAN
1
Exercise purpose
Experience and learn test-driven develo
pment based on test first
Main contents
Practice test-driven development with a sim
ple example
Test driven development of a f unction that m
eet the specifications
(C) 2007-2022 Hirohisa AMAN
2
Exercise content
Exercise 1
Experience test-driven development with a sim
ple example
Exercise 2
Complete f unctions that satisf y a given specific a given specific
ation though test driven development
(C) 2007-2022 Hirohisa AMAN
3
Exercise 1
[Target program] sample1101.c
[Support program] run_test.h
run_test.h provides functions to support
test-driven development exercises
Complete the program by a given specific editing sample
1101.c as explained below
(C) 2007-2022 Hirohisa AMAN
4
What is test-driven developmen
t?
Iterative technique to make “test case fir
st” Next, write a program that passes it write a program that passes it
The new test should fail the
first time (because it is not
implemented yet).
① Test case
creation
Review
Success
②Testin
g
Failure
Refactoring
(as needed)
Success
(C) 2007-2022 Hirohisa AMAN
③ Programming
④ Testing
5
Failure
Development goals f or Exercise
1
Make the function get_digit_number
This function return the number of digits f
or an integer x given as an argument
Where, 0< x <1000000 (=106) and
When given an integer outside the above
range, return -1 as an error
(Ex1) get_digit_number(256) …This value is 3
(Ex2) get_digit_number(0)
…This value is -1 (error)
(C) 2007-2022 Hirohisa AMAN
6
How to test in Exercise 1
Use the function run_test prepared here
Defined in the distributed run_test.h
Usage is as follows:
run_test(1,get_digit_number(5),1);
Test No.
(Use to display
results)
Calling
(executing)
the function
under test
(C) 2007-2022 Hirohisa AMAN
Expected results in
the test (Correct
return value)
7
Exercise 1
How to proceed (1/7)
First, prepare the first test case in the
main function (step ① )
At this point,
Function
get_digit_number()
does not exist
(C) 2007-2022 Hirohisa AMAN
8
Exercise 1
How to proceed (2/7)
Compile once and check that it will be
an error (Step ②)
In this case, the function get_digit_number doesn't exist
The correct answer is to get an error
((If there is no error here, write a program that passes it a f unction with that name is already a given specific.
This will confuse the subsequent tests.)
(C) 2007-2022 Hirohisa AMAN
9
Exercise 1
How to proceed (3/7)
Then prepare the function get_digit_n
umber: For now, write a program that passes it the test should pass i
f the return value is 1 set(Step ③ )
Currently, let's try to
make it work,
although it is "ad hoc"
(C) 2007-2022 Hirohisa AMAN
10
Exercise 1
How to proceed (4/7)
This time there is no compilation error,
and when we run it, it is displayed that
No.1 test passed (step ④ )
No. 1: The PASS indication is
provided by function run_test
(C) 2007-2022 Hirohisa AMAN
11
Exercise 1
How to proceed (5/7)
Add the f ollowing test cases (Step ① )
Give a two-digit integer (10) as an argume
nt as number 2, and make it a test case th
at expects the answer to be 2.
Add No.2
test case
(C) 2007-2022 Hirohisa AMAN
12
Exercise 1
How to proceed (6/7)
Compile and run again, this time, confir
m that the No.2 test f ails (Step ②)
No.1 remain PASS
No.2 is FAIL
The result (return value) of the function is 1 (result = 1)
Indication that the expected result is 2 (expected result = 2)
(C) 2007-2022 Hirohisa AMAN
13
Exercise 1
How to proceed (7/7)
Next, modify the program to pass both N
o.1 and No.2 tests (step ③ )
Test-driven development gradually impr
oves the degree of perfection of the pro
gram by repeating this process.
You can ref actor along the way a given specific
The point of this method is that the number
of test cases gradually a given specific increases, write a program that passes it but every a given specifict
hing is re-executed each time
(C) 2007-2022 Hirohisa AMAN
14
Repeat above steps
Repeat “Add test case & modify progra modif y a given specific progra
m” until y a given specificou can confirm that the f unct
ion works as specified.
Don't f orget error handling!
In the case of an error, -1 is the return value, b
ut it is not good to write -1 directly (it will be h
ard-coded), so it is better to define it as a macr
o called ERROR
(C) 2007-2022 Hirohisa AMAN
15
Submission of Exercise 1
Submit to Teams
sample1101.c
If you do pair programming
with two people, both of you
should submit the same thing.
[11] Exercise-2
Submission deadline: May 23,
13:00 (Vietnam time)
(C) 2007-2022 Hirohisa AMAN
16
Exercise 2
In the same way as Exercise 1
Complete the program sample1102.c
by the method
The specifications of this program are
spec1102.pdf
(C) 2007-2022 Hirohisa AMAN
17
Submission of Exercise 2
Submit to Teams
sample1102.c
If you do pair programming
with two people, both of you
should submit the same thing.
[11] Exercise-2
Submission deadline: May 29,
13:00 (Vietnam time)
(C) 2007-2022 Hirohisa AMAN
18
Things to note in Exercises 1 an
d2
Calling the test functions for each task
run_test(1,get_digit_number(5), 1);
run_test(2,get_digit_number(10), 2);
……………………
thinking about various thing and keep a
dding
One run_test becomes one test case, so
make as many a given specific as possible
(C) 2007-2022 Hirohisa AMAN
19
Survey a given specific
In this exercise, you can do it alone,
or you can do pair programming with
2 people.
I want to know which one you chose,
so please answer the survey first
[11] Survey (you must answer first)
(C) 2007-2022 Hirohisa AMAN
20