Recipes for unit testing in...

Preview:

Citation preview

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

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

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

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

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

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

7

Example TDD: From sqrt macro test to sqrt

macro

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.

9

Example TDD: From sqrt macro test to sqrt macro

The test:

10

Example TDD: From sqrt macro test to sqrt macro

The macro: version 1

11

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

12

Example TDD: From sqrt macro test to sqrt macro

The macro: version 2

13

Example TDD: Execution v2: sqrt calculated but…

14

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

15

Example TDD: Execution: final version

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

17

Sardana test suite execution

1

• 23 tests currently

• Some examples:-test_list: sar_demo against lsm …

-test_ascan: initial/final pos, intervals…

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

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

20

The team is…¡The Sardana community!

Testing team from now on: you all

Get involved, submit your tests to:sardana-devel@lists.sourceforge.net

SEP5 testing team till now•Carlos Pascual

•Zbigniew Reszela

•Carlos Falcón

•Gabriel Jover

•Marc Rosanes

21

Merci

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

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

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)

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

Recommended