automated software testing

25
tomated Software Testing ild Nilsen, May 2016

Upload: arild2

Post on 17-Jan-2017

50 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Automated Software Testing

Automated Software TestingArild Nilsen, May 2016

Page 2: Automated Software Testing

• Introduction• Dependency Injection• Tips & Tricks

Page 3: Automated Software Testing

automated software testing

test-driven development (TDD)

vs.

software tester

vs.

Page 4: Automated Software Testing

whole

syste

m

isolat

ion

integration test

unit test system testacceptance testend-to-end test

A/B test

Page 5: Automated Software Testing

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

Page 6: Automated Software Testing

Benefits of automated tests

• Catch regression errors• Drives the design of the code• Serves as documentation

Page 7: Automated Software Testing

Dependency Injection

Page 8: Automated Software Testing

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()

Page 9: Automated Software Testing

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()

Page 10: Automated Software Testing

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()

Page 11: Automated Software Testing

Testing with mocks

Page 12: Automated Software Testing

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

Page 13: Automated Software Testing

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()

Page 14: Automated Software Testing

Composition vs Inheritance

Page 15: Automated Software Testing

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)

Page 16: Automated Software Testing

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

Page 17: Automated Software Testing

Things that makes testing harder

• Inheritance• Static methods• Global state• Mixing of concerns• Top 10 things which make your code ha

rd to test

Page 18: Automated Software Testing

Tips & Tricks

Page 19: Automated Software Testing

See the test fail• Does it actually fail?• Is the error message descriptive?

Page 20: Automated Software Testing

@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));

}}

Page 21: Automated Software Testing

Naming and structuring tests

• Test features over methods• Some duplication is fine, but test

should read well

Page 22: Automated Software Testing

@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)));

}

Page 23: Automated Software Testing

Scope of test

Writing Great Unit Tests: Best and Worst Practice- Steve Sanderson, 2009

Page 24: Automated Software Testing

Testing too much• And’s , Or’s or But’s• Too many mocks

Page 25: Automated Software Testing

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