SlideShare une entreprise Scribd logo
1  sur  56
Télécharger pour lire hors ligne
HELP
MY Tests ARE
KILLING ME!!

brian swan @bgswan
follow me
on twitter
Contains code some
people may find offensive
Testsuite: org.springframework.util.StopWatchTests
Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.177 sec
Testcase: testValidUsage took 0.171 sec

FAILED
Unexpected timing 167
junit.framework.AssertionFailedError: Unexpected timing 167

at
org.springframework.util.StopWatchTests.testValidUsage(StopWatchTests.java:48)
public void testValidUsage() throws Exception {
StopWatch sw = new StopWatch();
long int1 = 166L;
long int2 = 45L;
String name1 = "Task 1";
String name2 = "Task 2";
assertFalse(sw.isRunning());
sw.start(name1);
Thread.sleep(int1);
assertTrue(sw.isRunning());
sw.stop();
long fudgeFactor = 0L;
assertTrue("Unexpected
assertTrue("Unexpected
sw.start(name2);
Thread.sleep(int2);
sw.stop();
assertTrue("Unexpected
assertTrue("Unexpected

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor);

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor);

assertTrue(sw.getTaskCount() == 2);
String pp = sw.prettyPrint();
assertTrue(pp.indexOf(name1) != -1);
assertTrue(pp.indexOf(name2) != -1);
StopWatch.TaskInfo[] tasks = sw.getTaskInfo();
assertTrue(tasks.length == 2);
assertTrue(tasks[0].getTaskName().equals(name1));
assertTrue(tasks[1].getTaskName().equals(name2));
sw.toString();
}
HH
GG
AR
A
public void testValidUsage() throws Exception {
StopWatch sw = new StopWatch();
long int1 = 166L;
long int2 = 45L;
String name1 = "Task 1";
String name2 = "Task 2";
assertFalse(sw.isRunning());
sw.start(name1);
Thread.sleep(int1);
assertTrue(sw.isRunning());
sw.stop();
// TODO are timings off in JUnit? Why do these assertions sometimes fail
// under both Ant and Eclipse?
//long fudgeFactor = 5L;
//assertTrue("Unexpected
//assertTrue("Unexpected
sw.start(name2);
Thread.sleep(int2);
sw.stop();
//assertTrue("Unexpected
//assertTrue("Unexpected

timing " + sw.getTotalTime(), sw.getTotalTime() >= int1);
timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + fudgeFactor);

timing " + sw.getTotalTime(), sw.getTotalTime() >= int1 + int2);
timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + int2 + fudgeFactor);

assertTrue(sw.getTaskCount() == 2);
String pp = sw.prettyPrint();
assertTrue(pp.indexOf(name1) != -1);
assertTrue(pp.indexOf(name2) != -1);
StopWatch.TaskInfo[] tasks = sw.getTaskInfo();
assertTrue(tasks.length == 2);
assertTrue(tasks[0].getTaskName().equals(name1));
assertTrue(tasks[1].getTaskName().equals(name2));
sw.toString();
}

https://github.com/SpringSource/spring-framework/blob/master/spring-core/src/test/java/org/springframework/util/
StopWatchTests.java
The UnREADABLE TEST
Long Test Methods featuring Undecipherable Failures
introducing Developer Confusion and Wasted Developer Time
produced by
Flesch Reading Ease

http://en.wikipedia.org/wiki/Flesch–Kincaid_Readability_Test
MORE READABILITY
Produced By

SMALL WORDS and SHORT SENTENCES
http://www.arrestedcomputing.com/pubs/readability-issta.pdf
http://www.arrestedcomputing.com/pubs/readability-issta.pdf
“ THE

LINE LENGTH
IDENTIFIER MASSACRE”
EDIBLE
HE INCR
T
it "adds items to top" do
stack = Stack.new
stack.add "thing"
assert_equal "thing", stack.top
stack = nil
end

#
#
#
#

Setup
Exercise
Verify
Teardown
it "adds items to top" do
stack = Stack.new
stack.add "thing"
assert_equal "thing", stack.top
end

# Setup
# Exercise
# Verify
it "adds items to top" do
stack = Stack.new
stack.add "thing"
assert_equal "thing", stack.top
end

# Arrange
# Act
# Assert
it "adds items to top" do
stack = Stack.new
stack.add "thing"
assert_equal "thing", stack.top
end

# Given
# When
# Then
before do
@stack = Stack.new
end

# Given

it "adds items to top" do
@stack.add "thing"
assert_equal "thing", @stack.top
end

# When
# Then
before do
@stack = Stack.new
end

# Given

it "is initially empty" do
assert_empty @stack # Then
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet = Timesheet.new
task_items = [stub(start_time: 1359291600, end_time: 1359295200),
stub(start_time: 1359295200, end_time: 1359298900)]
timesheet.stub(:task_items) { task_items }
timesheet.calculate_total_hours.should == 2.0
end

http://www.superpumpup.com/sub-ms-test
it "returns the total timesheet hours worked of the timesheet" do
timesheet = Timesheet.new
task_items = [stub(start_time: 1359291600, end_time: 1359295200),
stub(start_time: 1359295200, end_time: 1359298900)]
timesheet.stub(:task_items) { task_items }
timesheet.calculate_total_hours.should == 2.0
end
Extract Given
You have a long, confusing or poorly named test
setup.
Turn the fragment into a method or member variable
whose name describes the “given”.
http://martinfowler.com/bliki/ObjectMother.html
http://nat.truemesh.com/archives/000714.html
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
task_items = [stub(start_time: 1359291600, end_time: 1359295200),
stub(start_time: 1359295200, end_time: 1359298900)]
timesheet.stub(:task_items) { task_items }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
task_items = [stub(start_time: 1359291600, end_time: 1359295200),
stub(start_time: 1359295200, end_time: 1359298900)]
timesheet.stub(:task_items) { task_items }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
Extract Collaborator
An object your test depends on is confusing or
poorly named.
Extract the collaborating object into a method or
member variable whose name describes its purpose.
def one_hour_task
stub(start_time: 1359291600, end_time: 1359295200)
end
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
task_items = [one_hour_task, one_hour_task]
timesheet.stub(:task_items) { task_items }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
def one_hour_task
stub(start_time: 1359291600, end_time: 1359295200)
end
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
task_items = [one_hour_task, one_hour_task]
timesheet.stub(:task_items) { task_items }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
Inline Temp
You have a temp that is assigned to once with a
simple expression, and the temp is getting in the way
of other refactorings.
Replace all references to that temp with the
expression.
http://www.refactoring.com/catalog/inlineTemp.html
def one_hour_task
stub(start_time: 1359291600, end_time: 1359295200)
end
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
timesheet.stub(:task_items) { [one_hour_task, one_hour_task] }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
def one_hour_task
stub(start_time: 1359291600, end_time: 1359295200)
end
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
timesheet.stub(:task_items) { [one_hour_task, one_hour_task] }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
Use meaningful data
Your test uses arbitrary literal values as test data.
Replace arbitrary values with values that have
meaning in the domain of the test.
ONE_HOUR_IN_SECONDS = (60*60*1)
def one_hour_task
stub(start_time: 0, end_time: ONE_HOUR_IN_SECONDS)
end
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
timesheet.stub(:task_items) { [one_hour_task, one_hour_task] }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
ONE_HOUR_IN_SECONDS = (60*60*1)
def one_hour_task
stub(start_time: 0, end_time: ONE_HOUR_IN_SECONDS)
end
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
timesheet.stub(:task_items) { [one_hour_task, one_hour_task] }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
Extract Test
Your test method is checking multiple behaviours, or
checking behaviour of a collaborating object.
Extract a new test method for each behaviour.
describe Task do
it "knows duration in hours" do
task = Task.new(start_time: 0, end_time: 3600)
task.duration.should == 1.0
end
end
def one_hour_task
stub(duration: 1)
end
def timesheet_with_two_one_hour_tasks
timesheet = Timesheet.new
timesheet.stub(:task_items) { [one_hour_task, one_hour_task] }
timesheet
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0
end
it "returns the total timesheet hours worked of the timesheet" do
one_hour_task = stub(duration: 1)
timesheet = Timesheet.new
timesheet.stub(:task_items) { [one_hour_task, one_hour_task] }
timesheet.calculate_total_hours.should == 2.0
end
it "returns the total timesheet hours worked of the timesheet" do
timesheet = Timesheet.new
task_items = [stub(start_time: 1359291600, end_time: 1359295200),
stub(start_time: 1359295200, end_time: 1359298900)]
timesheet.stub(:task_items) { task_items }
timesheet.calculate_total_hours.should == 2.0
end

http://www.superpumpup.com/sub-ms-test
it "returns the total timesheet hours worked of the timesheet" do
one_hour_task = stub(duration: 1)
timesheet = Timesheet.new
timesheet.stub(:task_items) { [one_hour_task, one_hour_task] }
timesheet.calculate_total_hours.should == 2.0
end
The UnREADABLE TEST

II
public void testValidUsage() throws Exception {
StopWatch sw = new StopWatch();
long int1 = 166L;
long int2 = 45L;
String name1 = "Task 1";
String name2 = "Task 2";
assertFalse(sw.isRunning());
sw.start(name1);
Thread.sleep(int1);
assertTrue(sw.isRunning());
sw.stop();
long fudgeFactor = 0L;
assertTrue("Unexpected
assertTrue("Unexpected
sw.start(name2);
Thread.sleep(int2);
sw.stop();
assertTrue("Unexpected
assertTrue("Unexpected

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor);

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor);

assertTrue(sw.getTaskCount() == 2);
String pp = sw.prettyPrint();
assertTrue(pp.indexOf(name1) != -1);
assertTrue(pp.indexOf(name2) != -1);
StopWatch.TaskInfo[] tasks = sw.getTaskInfo();
assertTrue(tasks.length == 2);
assertTrue(tasks[0].getTaskName().equals(name1));
assertTrue(tasks[1].getTaskName().equals(name2));
sw.toString();
}
X

...

the Unknown
test case
“

clarity trumps brevity
in test names
https://www.facebook.com/notes/kent-beck/shorts-not-always-sweet-the-case-for-long-test-names/
564493423583526
Rename Method
The name of a method does not reveal its purpose.
Change the name of the method.

http://www.refactoring.com/catalog/renameMethod.html
public void testIsRunningWhenStartedAndTimeMultipleTasksAndTaskCountAndPrettyPrintAndTaskInfo() throws Exception {
StopWatch sw = new StopWatch();
long int1 = 166L;
long int2 = 45L;
String name1 = "Task 1";
String name2 = "Task 2";
assertFalse(sw.isRunning());
sw.start(name1);
Thread.sleep(int1);
assertTrue(sw.isRunning());
sw.stop();
long fudgeFactor = 5L;
assertTrue("Unexpected
assertTrue("Unexpected
sw.start(name2);
Thread.sleep(int2);
sw.stop();
assertTrue("Unexpected
assertTrue("Unexpected

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor);

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor);

assertTrue(sw.getTaskCount() == 2);
String pp = sw.prettyPrint();
assertTrue(pp.indexOf(name1) != -1);
assertTrue(pp.indexOf(name2) != -1);
StopWatch.TaskInfo[] tasks = sw.getTaskInfo();
assertTrue(tasks.length == 2);
assertTrue(tasks[0].getTaskName().equals(name1));
assertTrue(tasks[1].getTaskName().equals(name2));
sw.toString();
}
public void testTimeMultipleTasks() throws Exception {
StopWatch sw = new StopWatch();
long int1 = 166L;
long int2 = 45L;
String name1 = "Task 1";
String name2 = "Task 2";
sw.start(name1);
Thread.sleep(int1);
sw.stop();
long fudgeFactor = 5L;
assertTrue("Unexpected
assertTrue("Unexpected
sw.start(name2);
Thread.sleep(int2);
sw.stop();
assertTrue("Unexpected
assertTrue("Unexpected
}

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor);

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor);
public void testTimeMultipleTasks() throws Exception {
StopWatch sw = new StopWatch();
StopWatch.TaskInfo task1 = new StopWatch.TaskInfo("Task 1", 166L);
StopWatch.TaskInfo task2 = new StopWatch.TaskInfo("Task 2", 45L);
sw.start(task1.getTaskName());
Thread.sleep(task1.getTimeMillis());
sw.stop();
long fudgeFactor = 10L;
assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >=
task1.getTimeMillis());
assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <=
task1.getTimeMillis() + fudgeFactor);
sw.start(task2.getTaskName());
Thread.sleep(task2.getTimeMillis());
sw.stop();
assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >=
task1.getTimeMillis() + task2.getTimeMillis());
assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <=
task1.getTimeMillis() + task2.getTimeMillis() + fudgeFactor);
}
public void testTimeMultipleTasks() throws Exception {
StopWatch sw = new StopWatch();
StopWatch.TaskInfo task1 = new StopWatch.TaskInfo("Task 1", 166L);
StopWatch.TaskInfo task2 = new StopWatch.TaskInfo("Task 2", 45L);
timeTask(sw, task1);
long fudgeFactor = 10L;
assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >=
task1.getTimeMillis());
assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <=
task1.getTimeMillis() + fudgeFactor);
timeTask(sw, task2);
assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >=
task1.getTimeMillis() + task2.getTimeMillis());
assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <=
task1.getTimeMillis() + task2.getTimeMillis() + fudgeFactor);
}
private void timeTask(StopWatch sw, StopWatch.TaskInfo task) throws Exception {
sw.start(task.getTaskName());
Thread.sleep(task.getTimeMillis());
sw.stop();
}
Replace Assertion
Your test method uses a basic assertion to check for
a specific condition.
Use a more specific assertion provided by your test
framework, or create a custom assert method.
public void testTimeMultipleTasks() throws Exception {
StopWatch sw = new StopWatch();
StopWatch.TaskInfo task1 = new StopWatch.TaskInfo("Task 1", 166L);
StopWatch.TaskInfo task2 = new StopWatch.TaskInfo("Task 2", 45L);
timeTask(sw, task1);
assertElapsedTime(sw, task1.getTimeMillis());
timeTask(sw, task2);
assertElapsedTime(sw, task1.getTimeMillis() + task2.getTimeMillis());
}
private void assertElapsedTime(StopWatch sw, int expectedDuration) {
double tolerance = 10.0;
assertThat((double)sw.getTotalTimeMillis(), is(closeTo((double)expectedDuration, tolerance)));
}
private void timeTask(StopWatch sw, StopWatch.TaskInfo task) throws Exception {
sw.start(task.getTaskName());
Thread.sleep(task.getTimeMillis());
sw.stop();
}
public void testValidUsage() throws Exception {
StopWatch sw = new StopWatch();
long int1 = 166L;
long int2 = 45L;
String name1 = "Task 1";
String name2 = "Task 2";
assertFalse(sw.isRunning());
sw.start(name1);
Thread.sleep(int1);
assertTrue(sw.isRunning());
sw.stop();
long fudgeFactor = 0L;
assertTrue("Unexpected
assertTrue("Unexpected
sw.start(name2);
Thread.sleep(int2);
sw.stop();
assertTrue("Unexpected
assertTrue("Unexpected

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor);

timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2);
timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor);

assertTrue(sw.getTaskCount() == 2);
String pp = sw.prettyPrint();
assertTrue(pp.indexOf(name1) != -1);
assertTrue(pp.indexOf(name2) != -1);
StopWatch.TaskInfo[] tasks = sw.getTaskInfo();
assertTrue(tasks.length == 2);
assertTrue(tasks[0].getTaskName().equals(name1));
assertTrue(tasks[1].getTaskName().equals(name2));
sw.toString();
}
public void testTimeMultipleTasks() throws Exception {
StopWatch sw = new StopWatch();
StopWatch.TaskInfo task1 = new StopWatch.TaskInfo("Task 1", 166L);
StopWatch.TaskInfo task2 = new StopWatch.TaskInfo("Task 2", 45L);
timeTask(sw, task1);
assertElapsedTime(sw, task1.getTimeMillis());
timeTask(sw, task2);
assertElapsedTime(sw, task1.getTimeMillis() + task2.getTimeMillis());
}
Readability

Refactorings

Short Test Methods
Shorter Line Length
Fewer Identifiers
Clear Test Method Names
Extract Given
Extract Collaborator
Use Meaningful Data
Extract Test
Replace Assertion

Contenu connexe

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 

Dernier (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 

En vedette

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

En vedette (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Help, my tests are killing me!!

  • 1. HELP MY Tests ARE KILLING ME!! brian swan @bgswan follow me on twitter
  • 2. Contains code some people may find offensive
  • 3. Testsuite: org.springframework.util.StopWatchTests Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.177 sec Testcase: testValidUsage took 0.171 sec FAILED Unexpected timing 167 junit.framework.AssertionFailedError: Unexpected timing 167 at org.springframework.util.StopWatchTests.testValidUsage(StopWatchTests.java:48)
  • 4. public void testValidUsage() throws Exception { StopWatch sw = new StopWatch(); long int1 = 166L; long int2 = 45L; String name1 = "Task 1"; String name2 = "Task 2"; assertFalse(sw.isRunning()); sw.start(name1); Thread.sleep(int1); assertTrue(sw.isRunning()); sw.stop(); long fudgeFactor = 0L; assertTrue("Unexpected assertTrue("Unexpected sw.start(name2); Thread.sleep(int2); sw.stop(); assertTrue("Unexpected assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor); assertTrue(sw.getTaskCount() == 2); String pp = sw.prettyPrint(); assertTrue(pp.indexOf(name1) != -1); assertTrue(pp.indexOf(name2) != -1); StopWatch.TaskInfo[] tasks = sw.getTaskInfo(); assertTrue(tasks.length == 2); assertTrue(tasks[0].getTaskName().equals(name1)); assertTrue(tasks[1].getTaskName().equals(name2)); sw.toString(); }
  • 6. public void testValidUsage() throws Exception { StopWatch sw = new StopWatch(); long int1 = 166L; long int2 = 45L; String name1 = "Task 1"; String name2 = "Task 2"; assertFalse(sw.isRunning()); sw.start(name1); Thread.sleep(int1); assertTrue(sw.isRunning()); sw.stop(); // TODO are timings off in JUnit? Why do these assertions sometimes fail // under both Ant and Eclipse? //long fudgeFactor = 5L; //assertTrue("Unexpected //assertTrue("Unexpected sw.start(name2); Thread.sleep(int2); sw.stop(); //assertTrue("Unexpected //assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() >= int1); timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + fudgeFactor); timing " + sw.getTotalTime(), sw.getTotalTime() >= int1 + int2); timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + int2 + fudgeFactor); assertTrue(sw.getTaskCount() == 2); String pp = sw.prettyPrint(); assertTrue(pp.indexOf(name1) != -1); assertTrue(pp.indexOf(name2) != -1); StopWatch.TaskInfo[] tasks = sw.getTaskInfo(); assertTrue(tasks.length == 2); assertTrue(tasks[0].getTaskName().equals(name1)); assertTrue(tasks[1].getTaskName().equals(name2)); sw.toString(); } https://github.com/SpringSource/spring-framework/blob/master/spring-core/src/test/java/org/springframework/util/ StopWatchTests.java
  • 7. The UnREADABLE TEST Long Test Methods featuring Undecipherable Failures introducing Developer Confusion and Wasted Developer Time produced by
  • 9. MORE READABILITY Produced By SMALL WORDS and SHORT SENTENCES
  • 14. it "adds items to top" do stack = Stack.new stack.add "thing" assert_equal "thing", stack.top stack = nil end # # # # Setup Exercise Verify Teardown
  • 15. it "adds items to top" do stack = Stack.new stack.add "thing" assert_equal "thing", stack.top end # Setup # Exercise # Verify
  • 16. it "adds items to top" do stack = Stack.new stack.add "thing" assert_equal "thing", stack.top end # Arrange # Act # Assert
  • 17. it "adds items to top" do stack = Stack.new stack.add "thing" assert_equal "thing", stack.top end # Given # When # Then
  • 18. before do @stack = Stack.new end # Given it "adds items to top" do @stack.add "thing" assert_equal "thing", @stack.top end # When # Then
  • 19. before do @stack = Stack.new end # Given it "is initially empty" do assert_empty @stack # Then end
  • 20.
  • 21. it "returns the total timesheet hours worked of the timesheet" do timesheet = Timesheet.new task_items = [stub(start_time: 1359291600, end_time: 1359295200), stub(start_time: 1359295200, end_time: 1359298900)] timesheet.stub(:task_items) { task_items } timesheet.calculate_total_hours.should == 2.0 end http://www.superpumpup.com/sub-ms-test
  • 22. it "returns the total timesheet hours worked of the timesheet" do timesheet = Timesheet.new task_items = [stub(start_time: 1359291600, end_time: 1359295200), stub(start_time: 1359295200, end_time: 1359298900)] timesheet.stub(:task_items) { task_items } timesheet.calculate_total_hours.should == 2.0 end
  • 23. Extract Given You have a long, confusing or poorly named test setup. Turn the fragment into a method or member variable whose name describes the “given”.
  • 26. def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new task_items = [stub(start_time: 1359291600, end_time: 1359295200), stub(start_time: 1359295200, end_time: 1359298900)] timesheet.stub(:task_items) { task_items } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 27. def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new task_items = [stub(start_time: 1359291600, end_time: 1359295200), stub(start_time: 1359295200, end_time: 1359298900)] timesheet.stub(:task_items) { task_items } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 28. Extract Collaborator An object your test depends on is confusing or poorly named. Extract the collaborating object into a method or member variable whose name describes its purpose.
  • 29. def one_hour_task stub(start_time: 1359291600, end_time: 1359295200) end def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new task_items = [one_hour_task, one_hour_task] timesheet.stub(:task_items) { task_items } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 30. def one_hour_task stub(start_time: 1359291600, end_time: 1359295200) end def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new task_items = [one_hour_task, one_hour_task] timesheet.stub(:task_items) { task_items } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 31. Inline Temp You have a temp that is assigned to once with a simple expression, and the temp is getting in the way of other refactorings. Replace all references to that temp with the expression. http://www.refactoring.com/catalog/inlineTemp.html
  • 32. def one_hour_task stub(start_time: 1359291600, end_time: 1359295200) end def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new timesheet.stub(:task_items) { [one_hour_task, one_hour_task] } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 33. def one_hour_task stub(start_time: 1359291600, end_time: 1359295200) end def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new timesheet.stub(:task_items) { [one_hour_task, one_hour_task] } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 34. Use meaningful data Your test uses arbitrary literal values as test data. Replace arbitrary values with values that have meaning in the domain of the test.
  • 35. ONE_HOUR_IN_SECONDS = (60*60*1) def one_hour_task stub(start_time: 0, end_time: ONE_HOUR_IN_SECONDS) end def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new timesheet.stub(:task_items) { [one_hour_task, one_hour_task] } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 36. ONE_HOUR_IN_SECONDS = (60*60*1) def one_hour_task stub(start_time: 0, end_time: ONE_HOUR_IN_SECONDS) end def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new timesheet.stub(:task_items) { [one_hour_task, one_hour_task] } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 37. Extract Test Your test method is checking multiple behaviours, or checking behaviour of a collaborating object. Extract a new test method for each behaviour.
  • 38. describe Task do it "knows duration in hours" do task = Task.new(start_time: 0, end_time: 3600) task.duration.should == 1.0 end end
  • 39. def one_hour_task stub(duration: 1) end def timesheet_with_two_one_hour_tasks timesheet = Timesheet.new timesheet.stub(:task_items) { [one_hour_task, one_hour_task] } timesheet end it "returns the total timesheet hours worked of the timesheet" do timesheet_with_two_one_hour_tasks.calculate_total_hours.should == 2.0 end
  • 40. it "returns the total timesheet hours worked of the timesheet" do one_hour_task = stub(duration: 1) timesheet = Timesheet.new timesheet.stub(:task_items) { [one_hour_task, one_hour_task] } timesheet.calculate_total_hours.should == 2.0 end
  • 41. it "returns the total timesheet hours worked of the timesheet" do timesheet = Timesheet.new task_items = [stub(start_time: 1359291600, end_time: 1359295200), stub(start_time: 1359295200, end_time: 1359298900)] timesheet.stub(:task_items) { task_items } timesheet.calculate_total_hours.should == 2.0 end http://www.superpumpup.com/sub-ms-test
  • 42. it "returns the total timesheet hours worked of the timesheet" do one_hour_task = stub(duration: 1) timesheet = Timesheet.new timesheet.stub(:task_items) { [one_hour_task, one_hour_task] } timesheet.calculate_total_hours.should == 2.0 end
  • 44. public void testValidUsage() throws Exception { StopWatch sw = new StopWatch(); long int1 = 166L; long int2 = 45L; String name1 = "Task 1"; String name2 = "Task 2"; assertFalse(sw.isRunning()); sw.start(name1); Thread.sleep(int1); assertTrue(sw.isRunning()); sw.stop(); long fudgeFactor = 0L; assertTrue("Unexpected assertTrue("Unexpected sw.start(name2); Thread.sleep(int2); sw.stop(); assertTrue("Unexpected assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor); assertTrue(sw.getTaskCount() == 2); String pp = sw.prettyPrint(); assertTrue(pp.indexOf(name1) != -1); assertTrue(pp.indexOf(name2) != -1); StopWatch.TaskInfo[] tasks = sw.getTaskInfo(); assertTrue(tasks.length == 2); assertTrue(tasks[0].getTaskName().equals(name1)); assertTrue(tasks[1].getTaskName().equals(name2)); sw.toString(); }
  • 46. “ clarity trumps brevity in test names https://www.facebook.com/notes/kent-beck/shorts-not-always-sweet-the-case-for-long-test-names/ 564493423583526
  • 47. Rename Method The name of a method does not reveal its purpose. Change the name of the method. http://www.refactoring.com/catalog/renameMethod.html
  • 48. public void testIsRunningWhenStartedAndTimeMultipleTasksAndTaskCountAndPrettyPrintAndTaskInfo() throws Exception { StopWatch sw = new StopWatch(); long int1 = 166L; long int2 = 45L; String name1 = "Task 1"; String name2 = "Task 2"; assertFalse(sw.isRunning()); sw.start(name1); Thread.sleep(int1); assertTrue(sw.isRunning()); sw.stop(); long fudgeFactor = 5L; assertTrue("Unexpected assertTrue("Unexpected sw.start(name2); Thread.sleep(int2); sw.stop(); assertTrue("Unexpected assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor); assertTrue(sw.getTaskCount() == 2); String pp = sw.prettyPrint(); assertTrue(pp.indexOf(name1) != -1); assertTrue(pp.indexOf(name2) != -1); StopWatch.TaskInfo[] tasks = sw.getTaskInfo(); assertTrue(tasks.length == 2); assertTrue(tasks[0].getTaskName().equals(name1)); assertTrue(tasks[1].getTaskName().equals(name2)); sw.toString(); }
  • 49. public void testTimeMultipleTasks() throws Exception { StopWatch sw = new StopWatch(); long int1 = 166L; long int2 = 45L; String name1 = "Task 1"; String name2 = "Task 2"; sw.start(name1); Thread.sleep(int1); sw.stop(); long fudgeFactor = 5L; assertTrue("Unexpected assertTrue("Unexpected sw.start(name2); Thread.sleep(int2); sw.stop(); assertTrue("Unexpected assertTrue("Unexpected } timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor);
  • 50. public void testTimeMultipleTasks() throws Exception { StopWatch sw = new StopWatch(); StopWatch.TaskInfo task1 = new StopWatch.TaskInfo("Task 1", 166L); StopWatch.TaskInfo task2 = new StopWatch.TaskInfo("Task 2", 45L); sw.start(task1.getTaskName()); Thread.sleep(task1.getTimeMillis()); sw.stop(); long fudgeFactor = 10L; assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= task1.getTimeMillis()); assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= task1.getTimeMillis() + fudgeFactor); sw.start(task2.getTaskName()); Thread.sleep(task2.getTimeMillis()); sw.stop(); assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= task1.getTimeMillis() + task2.getTimeMillis()); assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= task1.getTimeMillis() + task2.getTimeMillis() + fudgeFactor); }
  • 51. public void testTimeMultipleTasks() throws Exception { StopWatch sw = new StopWatch(); StopWatch.TaskInfo task1 = new StopWatch.TaskInfo("Task 1", 166L); StopWatch.TaskInfo task2 = new StopWatch.TaskInfo("Task 2", 45L); timeTask(sw, task1); long fudgeFactor = 10L; assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= task1.getTimeMillis()); assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= task1.getTimeMillis() + fudgeFactor); timeTask(sw, task2); assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= task1.getTimeMillis() + task2.getTimeMillis()); assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= task1.getTimeMillis() + task2.getTimeMillis() + fudgeFactor); } private void timeTask(StopWatch sw, StopWatch.TaskInfo task) throws Exception { sw.start(task.getTaskName()); Thread.sleep(task.getTimeMillis()); sw.stop(); }
  • 52. Replace Assertion Your test method uses a basic assertion to check for a specific condition. Use a more specific assertion provided by your test framework, or create a custom assert method.
  • 53. public void testTimeMultipleTasks() throws Exception { StopWatch sw = new StopWatch(); StopWatch.TaskInfo task1 = new StopWatch.TaskInfo("Task 1", 166L); StopWatch.TaskInfo task2 = new StopWatch.TaskInfo("Task 2", 45L); timeTask(sw, task1); assertElapsedTime(sw, task1.getTimeMillis()); timeTask(sw, task2); assertElapsedTime(sw, task1.getTimeMillis() + task2.getTimeMillis()); } private void assertElapsedTime(StopWatch sw, int expectedDuration) { double tolerance = 10.0; assertThat((double)sw.getTotalTimeMillis(), is(closeTo((double)expectedDuration, tolerance))); } private void timeTask(StopWatch sw, StopWatch.TaskInfo task) throws Exception { sw.start(task.getTaskName()); Thread.sleep(task.getTimeMillis()); sw.stop(); }
  • 54. public void testValidUsage() throws Exception { StopWatch sw = new StopWatch(); long int1 = 166L; long int2 = 45L; String name1 = "Task 1"; String name2 = "Task 2"; assertFalse(sw.isRunning()); sw.start(name1); Thread.sleep(int1); assertTrue(sw.isRunning()); sw.stop(); long fudgeFactor = 0L; assertTrue("Unexpected assertTrue("Unexpected sw.start(name2); Thread.sleep(int2); sw.stop(); assertTrue("Unexpected assertTrue("Unexpected timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + fudgeFactor); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() >= int1 + int2); timing " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() <= int1 + int2 + fudgeFactor); assertTrue(sw.getTaskCount() == 2); String pp = sw.prettyPrint(); assertTrue(pp.indexOf(name1) != -1); assertTrue(pp.indexOf(name2) != -1); StopWatch.TaskInfo[] tasks = sw.getTaskInfo(); assertTrue(tasks.length == 2); assertTrue(tasks[0].getTaskName().equals(name1)); assertTrue(tasks[1].getTaskName().equals(name2)); sw.toString(); }
  • 55. public void testTimeMultipleTasks() throws Exception { StopWatch sw = new StopWatch(); StopWatch.TaskInfo task1 = new StopWatch.TaskInfo("Task 1", 166L); StopWatch.TaskInfo task2 = new StopWatch.TaskInfo("Task 2", 45L); timeTask(sw, task1); assertElapsedTime(sw, task1.getTimeMillis()); timeTask(sw, task2); assertElapsedTime(sw, task1.getTimeMillis() + task2.getTimeMillis()); }
  • 56. Readability Refactorings Short Test Methods Shorter Line Length Fewer Identifiers Clear Test Method Names Extract Given Extract Collaborator Use Meaningful Data Extract Test Replace Assertion