automated software testing
Post on 17-Jan-2017
50 Views
Preview:
TRANSCRIPT
Automated Software TestingArild Nilsen, May 2016
• Introduction• Dependency Injection• Tips & Tricks
automated software testing
test-driven development (TDD)
vs.
software tester
vs.
whole
syste
m
isolat
ion
integration test
unit test system testacceptance testend-to-end test
A/B test
Should you write tests?
- Truth about booking.com
“Booking is destroying my career (…)I am not allowed to write tests”
“I don’t do tests”- David Thomas
A/B testingSome unit tests
Very experiencedWrites tests for complex algorithm
Benefits of automated tests
• Catch regression errors• Drives the design of the code• Serves as documentation
Dependency Injection
class Poller: def __init__(self): self.service = Service()
def wait_for_completed(self): while not self.service.is_done(): sleep(10)
Poller().wait_for_completed()
class Poller: def __init__(self, service, poll_delay): self.service = service self.poll_delay = poll_delay
def wait_for_completed(self): while not self.service.is_done(): sleep(self.poll_delay)
Poller(Service(), 10).wait_for_completed()
class Poller: def set_service(self, service): self.service = service
def set_poll_delay(self, poll_delay): self.poll_delay = poll_delay
def wait_for_completed(self): while not self.service.is_done(): sleep(self.poll_delay)
poller = Poller()poller.set_service(Service())poller.set_poll_delay(10)poller.wait_for_completed()
Testing with mocks
class ServiceMock(Service): def __init__(self): self.called = 0
def is_done(self): self.called += 1 return self.called == 2
def terminates_when_service_is_done(): mock = ServiceMock() Poller(mock, 0).wait_for_completed()
assert 2 is mock.called
def terminates_when_service_is_done(): mock = Mock(Service)
when(mock.is_done()) .thenReturn(False) .thenReturn(True)
Poller(mock, 0).wait_for_completed()
verify(mock, times(2)).is_done()
Composition vs Inheritance
class Poller(Service): def __init__(self, poll_delay): self.poll_delay = poll_delay
def wait_for_completed(self): while not self.is_done(): sleep(self.poll_delay)
class PollerTest(Poller): def __init__(self, poll_delay): super(PollerTest,self)
.__init__(poll_delay) self.called = 0
def is_done(self): self.called += 1 return self.called == 2
def terminates_when_service_is_done(): poller = PollerTest(poll_delay=0)
poller.wait_for_completed() assert 2 is poller.called
Things that makes testing harder
• Inheritance• Static methods• Global state• Mixing of concerns• Top 10 things which make your code ha
rd to test
Tips & Tricks
See the test fail• Does it actually fail?• Is the error message descriptive?
@Testpublic void readsFile() throws VodFileStorageException {
try (InputStream stream = new InputStream("clip.mp4")) {ftpFileStorage.writeFile("tmp.mp4", stream);
}
try (InputStream expected = new InputStream("clip.mp4"))) {InputStream actual = ftpFileStorage.readFile("tmp.mp4");contentEquals(expected, actual);
}}
@Testpublic void readsFile() throws VodFileStorageException {
try (InputStream stream = new InputStream("clip.mp4")) {ftpFileStorage.writeFile("tmp.mp4", stream);
}
try (InputStream expected = new InputStream("clip.mp4"))) {InputStream actual = ftpFileStorage.readFile("tmp.mp4");assertThat(contentEquals(expected, actual), is(true));
}}
Naming and structuring tests
• Test features over methods• Some duplication is fine, but test
should read well
@MockFolderWatchService service;
@Testpublic void addsJobId() throws Exception {
FolderWatch folderWatch = createFolderWatch().jobIds(newHashSet()).build();
JobId id1 = createJob().build().getId();JobId id2 = createJob().build().getId();
folderWatch.addJobId(service, id1);verify(service).addJobId(folderWatch.getId(), id1);assertThat(folderWatch.getJobIds(), is(newHashSet(id1)));
folderWatch.addJobId(service, id2);verify(service).addJobId(folderWatch.getId(), id2);assertThat(folderWatch.getJobIds(), is(newHashSet(id1, id2)));
}
Scope of test
Writing Great Unit Tests: Best and Worst Practice- Steve Sanderson, 2009
Testing too much• And’s , Or’s or But’s• Too many mocks
Where to learn more?Inversion of Control Containers and the Dependency Injection pattern- Martin Fowler, 2004Writing Great Unit Tests:Best and Worst Practice- Steve Sanderson, 2009
top related