test driven development for invisible hardware - kdab · 2015-03-17 · unit test each class, e.g....

26
Test driven development for Invisible Hardware Samuel Gaist Edeltech Ltd Switzerland jeudi, 9 octobre 14

Upload: others

Post on 12-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

Test driven development for Invisible Hardware

Samuel Gaist

Edeltech LtdSwitzerland

jeudi, 9 octobre 14

Page 2: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

Samuel Gaist

Software Development Engineer• Partner at Edeltech Ltd

• Developer at IDIAP Research Institute

Qt Experience• User since Qt 3.2

• Contributor for ~ 2 years (QtCore, QtGui, QtMacExtras)

• 10’000+ posts in Qt Forum

2

jeudi, 9 octobre 14

Page 3: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

Outline

1. AgiScan, a film scanning application

2. Invisible Devices

3. Test Driven Development with Invisible Devices

4. Demo

5. Wrap Up

6. Q/A

jeudi, 9 octobre 14

Page 4: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

1. AgiScan, a film scanning application

Embedded AppProprietary

Qt Desktop AppMac OS / Windows

USB

Audio/Video

jeudi, 9 octobre 14

Page 5: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

1. AgiScan, a film scanning application

jeudi, 9 octobre 14

Page 6: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

1. AgiScan, a film scanning application

The Application (AgiScan) requires the device (Film scanner).

• to setup the GUI since the App supports different scanner

models with different hardware options.

• to setup the audio and video capture subsystems since

each scanner model produces different resolutions, image

formats, etc…

jeudi, 9 octobre 14

Page 7: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

2. Invisible Devices

• It can’t be moved next to your desk. because it’s too big or heavy

• It doesn’t exist yet. next week, promised

• It exists but you can’t have it. because someone needs it or it’s too expensive

• The only one we have has been sold yesterday. we’ll build you a new one if we get an order…

200k€200kg

jeudi, 9 octobre 14

Page 8: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

2. Invisible Devices

I can’t have one = INVISIBLE

So how do I develop my Application ?

jeudi, 9 octobre 14

Page 9: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

3. Test Driven Development with Invisible Devices

Write a Mock Objectto mimic the behaviour of the Invisible Device

in controlled ways.

jeudi, 9 octobre 14

Page 10: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

3. Test Driven Development with Invisible Devices

Use an Adapter Objectto communicate with the device when it is available,

or with the Mock Object.

jeudi, 9 octobre 14

Page 11: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

3. Test Driven Development with Invisible Devices

Apply TDD principlesbecause we want to sleep well.

jeudi, 9 octobre 14

Page 12: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

3. Test Driven Development with Invisible Devices

Run Test

(Re-)Write a Test

Run all Tests

Write Code

Refactor / Clean Code

PASS

FAIL

FAIL

PASS

TDD Friendly Reminder

http://en.wikipedia.org/wiki/Test-driven_development

jeudi, 9 octobre 14

Page 13: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

3. Test Driven Development with Invisible Devices

TDD Benefits1. Know how your code works.

2. Know that your code works.

3. Help other developers understand your code.

4. Know when you break something.

jeudi, 9 octobre 14

Page 14: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

Levels of testingUNIT TEST

Each class, e.g. the Mock Object

FUNCTIONAL TEST

Several classes, e.g. Mock Object + Adapter

SYSTEM TEST

Application: e.g. Scenario based user interaction, Squish

14

3. Test Driven Development with Invisible Devices

jeudi, 9 octobre 14

Page 15: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

15

3. Test Driven Development with Invisible Devices

Project Architecture

jeudi, 9 octobre 14

Page 16: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

#include <QtTest>#include <QCoreApplication>

#include "mycoolclass.h"

class UnitTestMyCoolClass : public QObject{ Q_OBJECT

private Q_SLOTS: void testCase1();};

QTEST_MAIN(UnitTestMyCoolClass);

#include “tst_UnitTestMyCoolClass.moc”16

3. Test Driven Development with Invisible Devices

Using QTest

jeudi, 9 octobre 14

Page 17: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

17

3. Test Driven Development with Invisible Devices

Let’s write a unit test.

jeudi, 9 octobre 14

Page 18: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

18

3. Test Driven Development with Invisible Devices

Know your Device1. What interface does it provide ?

Serial Port, Network, …

2. What is the communication protocol ?Commands, Responses

To write the Mock Object.

jeudi, 9 octobre 14

Page 19: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

19

The Mock Object

#include <QObject>

class MockDevice : public QIODevice{ Q_OBJECT

public: MockDevice();

protected: qint64 readData(char *data, qint64 maxlen) Q_DECL_OVERRIDE; qint64 writeData(const char *data, qint64 len) Q_DECL_OVERRIDE;};

3. Test Driven Development with Invisible Devices

jeudi, 9 octobre 14

Page 20: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

20

3. Test Driven Development with Invisible Devices

Define the Controller API1. How will the application interact with

the controller ? Signal/Slots, Events, other ?

2. Level of abstraction Map the “device lingo” to sensible methods / properties.

To write the Adapter Object.

jeudi, 9 octobre 14

Page 21: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

21

#include <QObject>

class Controller : public QObject{ Q_OBJECT

public: Controller(QObject *parent = 0);

void setDevice(QIODevice *device);

public Q_SLOTS: void doSomething(); void doSomethingElse();

private: QIODevice *_device;};

void Controller::doSomething(){ _device->write(DoSomethingCommand);}

QIODevice QBuffer, QSocket, QSerialPort…

The Adapter Object

3. Test Driven Development with Invisible Devices

jeudi, 9 octobre 14

Page 22: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

22

3. Test Driven Development with Invisible Devices

Let’s write a controller and test it.

jeudi, 9 octobre 14

Page 23: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

23

3. Test Driven Development with Invisible Devices

Detect the actual deviceUsing an enumerator (Bonjour, QSerialPortInfo)

Write a Factoryto return the Actual Device or the Mock Device.

and pass it to your Controller using ::setDevice(QIODevice*)

jeudi, 9 octobre 14

Page 24: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

24

4. Demo

Demo

jeudi, 9 octobre 14

Page 25: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

25

5. Wrap up

• Using a Mock Device we can develop an application controlling a device without the actual hardware.

• The Tests ensure that the Mock Device and the Application work correctly, and allow developers to reproduce bugs occurring with the actual hardware.

• The Mock Device can be used to create a Demo Mode.

jeudi, 9 octobre 14

Page 26: Test driven development for Invisible Hardware - KDAB · 2015-03-17 · UNIT TEST Each class, e.g. the Mock Object FUNCTIONAL TEST Several classes, e.g. Mock Object + Adapter SYSTEM

Q/A

Questions ?

26

jeudi, 9 octobre 14