recipes for unit testing in...

25
1 Recipes for unit testing in Sardana/Taurus SEP 5 Guidelines for Testing Sardana/Taurus Code Paving the way towards test driven development Marc Rosanes

Upload: others

Post on 24-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

1

Recipes for unit testing in Sardana/Taurus

SEP 5 Guidelines for Testing Sardana/Taurus Code

Paving the way towards test driven development

Marc Rosanes

Page 2: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

2

Contents• Benefits of testing

• Base framework: PyUnit unittest

• Organization and naming conventions

• Keypoints testing in Sardana/Taurus

• Example of TDD

• Test execution

• Documentation & Links

• The team

Page 3: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

3

Benefits of unit testing & TDD• Unit-Tests allows test-driven development: TDD

– Code proven to meet requirements

– Quick feedback at each step: focus on tasks

– Less time to debug

– Catch defects early in the development cycle

– Tests acts as documentation

Page 4: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

4

PyUnit: unittest• Module unittest• Base framework used for Sardana testing• Doc: https://docs.python.org/2/library/unittest.html• Unittest provides:

– Many assert methods that eases the tests– Test methods autodiscovery– Option to run independent tests or a whole testsuite– Specific methods for setting Up (setUp) and tidying up

(tearDown) tests

Page 5: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

5

Organization and namingAllowing test autodiscovery

• Sardana folders and subfolders

AnySardanafolder

test

• Unit Tests test_module_name

• Integration test_functionality

• utils no name convention (no test_)

res• resources:

no name convention (no test_)

Page 6: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

6

Keypoints when testing in Sardana/TaurusAllowing test autodiscovery

• from taurus.external import unittest• Only Test Classes (classes executing test methods)

must inherit from: unittest.TestCase• Test methods must begin by the prefix test• Test documentation written at the Module, Class

and Method docstring using sphinx formating.• Method docstring is used in unittest summaries• Use sar_demo and SarDemoEnv• New elements (e.g. motors) removed at the end

Page 7: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

7

Example TDD: From sqrt macro test to sqrt

macro

Page 8: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

8

Example of Sardana TDD:From sqrt macro test to sqrt

macro• The Requirements:

•Write a macro named “sqrtmac”. Its output (‘out’) must be the square root of the input data (‘in’).

•Its data must be given in the form {‘in’:x,’out’:s}

•Macro must raise an Exception of type ValueError if negative numbers are given as input.

Page 9: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

9

Example TDD: From sqrt macro test to sqrt macro

The test:

Page 10: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

10

Example TDD: From sqrt macro test to sqrt macro

The macro: version 1

Page 11: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

11

Example TDD: Execution v1: No code, both tests fails

Page 12: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

12

Example TDD: From sqrt macro test to sqrt macro

The macro: version 2

Page 13: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

13

Example TDD: Execution v2: sqrt calculated but…

Page 14: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

14

Example TDD: From sqrt macro test to sqrt macroThe macro: final version

Page 15: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

15

Example TDD: Execution: final version

Page 16: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

16

Test execution• Execution of a single test

python –m unittest –v test_name• Sardana testsuite execution

Go to: <sardana>/src/sardana/test/.

And do: python testsuite.py• Taurus testsuite execution

Go to: <taurus>/lib/taurus/test/.

And do: python testsuite.py

Page 17: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

17

Sardana test suite execution

1

• 23 tests currently

• Some examples:-test_list: sar_demo against lsm …

-test_ascan: initial/final pos, intervals…

Page 18: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

18

Documentation & linksabout Sardana testing

SEP 5 WIKIhttps://sourceforge.net/p/sardana/wiki/SEP5/

Sardana project documentation: https://sourceforge.net/p/sardana/sardana.git/

Build doc: python setup.py build_docGo to:

/sardana/build/sphinx/html/devel/howto_test/index.htmlsardana/build/sphinx/html/devel/api/api_test.html

Page 19: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

19

To be retained about SEP5

• Defines guidelines and provides examples• Code Coverage of around 10-20%• Taurus: Utils for testing widgets developed• Sardana: Utils for testing macros developed• GIT: develop branch• We invite people to contribute with tests• Efforts: testing Core and Controllers

Page 20: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

20

The team is…¡The Sardana community!

Testing team from now on: you all

Get involved, submit your tests to:[email protected]

SEP5 testing team till now•Carlos Pascual

•Zbigniew Reszela

•Carlos Falcón

•Gabriel Jover

•Marc Rosanes

Page 21: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

21

Merci

Page 22: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

22

• Tests must be portable: runnable for everybody

• Elements created by the sar_demo macro

• Specific elements must be removed at the end of the test (e.g. using the tearDown method).

• Class SarDemoEnv: getting sar_demo elements

- getElements(elem_type=‘pseudomotor’)

- getMotors()

- getControllers() …

Testing macros Getting the elements of sar_demo

Page 23: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

23

Testing macros Utils: MacroExecutor

• Module sardana.macroserver.macros.test

• Macro executor

• macro_executor.run()- macro_name, macro_params, timeout

• macro_executor.stop()

• RegisterAll(), unregisterAll():- Register for macro result and all log levels

Page 24: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

24

Testing macros Base Classes and helper methods

• Macro test classes can inherit from classes which simplifies macro execution and stop:- BaseMacroTestCase

- Creates a Tango Macro Executor

- RunMacroTestCase- Helpers: macro_runs(), macro_fails()

- RunStopMacroTestCase- Helpers: macro_stops()

- macro_stops(self, macro_params=None, stop_delay=0.1, wait_timeout=10)

Page 25: Recipes for unit testing in Sardana/Taurussourceforge.net/p/sardana/wiki/2ndSardanaMeeting/attachment/2nd… · Benefits of unit testing & TDD • Unit-Tests allows test-driven development:

25

Testing macros Decorators

• Decorator: @macroTest• API for new tests based on a helper method

• Same macro test with multiple inputs sets

• macroTest(helper_name, test_method_name, test_method_doc)

• Base decorator used to create other decorators as:- @testRun: @macroTest helper_name=‘macro_runs’

- @testStop: @macroTest helper_name=‘macro_stops’

- @testFail: @macroTest helper_name=‘macro_fails’

- Other customized decorators can be created

• @macroTest assumes macro_name is defined