When I write a test case, I get a story about how much coverage is covered.
What is code coverage? I thought, so I will summarize it as my memorandum.
In software testing, all program code (internal logic) to be tested It is the ratio (coverage rate) of the part of the body that has been tested. Reference
Each statement is executed at least once
Execute at least once for each judgment condition
Authenticity in each conditional statement is executed at least once
All combinations of truth and falsehood that can be placed in each condition are executed
sample.rb
module CodeCoverage
class << self
# main
# @params [interger] x
# @params [interger] y
# @return [String]Strings that differ depending on the values of x and y
#
#Argument assumes only interger
def main(x, y)
result = ''
if x >= y || x % 2 == 0
result << 'Process 1'
else
result << 'Process 2'
end
if x*y > 10
result << 'Process 3'
else
result << 'Process 4'
end
return result
end
end
end
One of the frameworks to realize TDD (Test Driven Development) which is one of the development methods of Ruby on Rails Reference
RSpec.describe CodeCoverage do
shared_examples 'Process 1 and process 3 pass' do
it 'The wording of process 1 and process 3 is returned' do
expect(subject).to eq 'Process 1 Process 3'
end
end
shared_examples 'Process 1 and process 4 pass' do
it 'The wording of process 1 and process 4 is returned' do
expect(subject).to eq 'Process 1 Process 4'
end
end
shared_examples 'Process 2 and process 3 pass' do
it 'The wording of process 2 and process 3 is returned' do
expect(subject).to eq 'Process 2 Process 3'
end
end
shared_examples 'Process 2 and process 4 pass' do
it 'The wording of process 2 and process 4 is returned' do
expect(subject).to eq 'Process 2 Process 4'
end
end
describe 'main' do
subject { described_class.send(:main, x, y) }
# C0:Instruction coverage
#Write to execute at least once with few instructions
context 'For C0' do
context 'Go through process 1 and process 3' do
let(:x) { 11 }
let(:y) { 1 }
it_behaves_like 'Process 1 and process 3 pass'
end
context 'Go through process 2 and process 4' do
let(:x) { 1 }
let(:y) { 9 }
it_behaves_like 'Process 2 and process 4 pass'
end
end
# C1:Branch coverage
#Execute at least once for each judgment condition
context 'For C1' do
context 'Go through process 1 and process 3' do
let(:x) { 11 }
let(:y) { 1 }
it_behaves_like 'Process 1 and process 3 pass'
end
context 'Go through process 1 and process 4' do
let(:x) { 3 }
let(:y) { 2 }
it_behaves_like 'Process 1 and process 4 pass'
end
context 'Go through process 2 and process 3' do
let(:x) { 3 }
let(:y) { 5 }
it_behaves_like 'Process 2 and process 3 pass'
end
context 'Go through process 2 and process 4' do
let(:x) { 1 }
let(:y) { 4 }
it_behaves_like 'Process 2 and process 4 pass'
end
end
# C2:Condition coverage
#Authenticity in each conditional statement is executed at least once
context 'For C2' do
context 'x>=y and x*y>10' do
context 'Go through process 1 and process 3' do
let(:x) { 11 }
let(:y) { 1 }
it_behaves_like 'Process 1 and process 3 pass'
end
end
context 'x>=y and x*y<10' do
context 'Go through process 1 and process 4' do
let(:x) { 3 }
let(:y) { 2 }
it_behaves_like 'Process 1 and process 4 pass'
end
end
context 'x<y and x are even and x*y>10' do
context 'Go through process 1 and process 3' do
let(:x) { 4 }
let(:y) { 5 }
it_behaves_like 'Process 1 and process 3 pass'
end
end
context 'x<y and x are even and x*y<10' do
context 'Go through process 1 and process 4' do
let(:x) { 2 }
let(:y) { 3 }
it_behaves_like 'Process 1 and process 4 pass'
end
end
context 'x<y and x are odd and x*y>10' do
context 'Go through process 2 and process 3' do
let(:x) { 3 }
let(:y) { 5 }
it_behaves_like 'Process 2 and process 3 pass'
end
end
context 'x<y and x are odd and x*y<10' do
context 'Go through process 2 and process 4' do
let(:x) { 1 }
let(:y) { 4 }
it_behaves_like 'Process 2 and process 4 pass'
end
end
end
end
end
end
It's easy to understand if you write it like this. How much code coverage is satisfied is appropriate, but at work it is up to about C1.
Recommended Posts