2015-03-28 - 2015-03-29 BDD Hands-on with Python 1
BBD Hands-on with Python
Practical Hands-on Workshop about"Behaviour Driven Development", implementing
the Game "CodeBreaker" on Python 2.7 as Example
PythonCamp Cologne, 2015-03-28 - 2015-03-29
Lecturer: Rolf Hemmerling
Slides: http://www.slideshare.net/hemmerling/License: Creative Commons - Attribution-ShareAlike 4.0 Generic ( http://www.creativecommons.org/licenses/by-sa/4.0/ )
2015-03-28 - 2015-03-29 BDD Hands-on with Python 2
Agenda
1. CodeBreaker – The Game
2. Gherkin – The BDD Language
3. GUI Design Process
4. Coding..
5. PEP8 Compliance
6. Setup by distutils, Installation by setup.py, Publication on GitHub, Test & Correction
7. Project Feedback
8. Project Roadmap
9. Resources
2015-03-28 - 2015-03-29 BDD Hands-on with Python 3
1 CodeBreaker – The Game
● Goal: You have to guess a 4-digit secret number
● Howto: You get hints after each guess
● "+" = Right Number at right place● "-" = Right number, but wrong place
● End of Game: If you got the right number
Example:
Secret Number = "1134"
Guess = "5115"
Hint = "+-"
2015-03-28 - 2015-03-29 BDD Hands-on with Python 4
2.1 Gherkin – The BDD Language
Feature: code-breaker starts game
As a code-breaker
I want to start a game
So that I can break the code
@StartGame
Scenario: start game
Given I am not yet playing
When I start a new game
Then I should see "Welcome to Codebreaker!"
And I should see "Enter guess:"
2015-03-28 - 2015-03-29 BDD Hands-on with Python 5
2.2 Gherkin – The BDD Language
Feature: code-breaker submits guess
Scenario Outline: submit guess
Given the secret code is "<code>"
When I guess "<guess>"
Then the mark should be "<mark>"
Scenarios: no matches
| code | guess | mark |
| 1234 | 5555 | |
Scenarios: 1 number correct
| code | guess | mark |
| 1234 | 1555 | + |
| 1234 | 2555 | - |
2015-03-28 - 2015-03-29 BDD Hands-on with Python 6
2.3 BDD Process, Step #1
.feature File BDD source File code
Native language Executable test code
2015-03-28 - 2015-03-29 BDD Hands-on with Python 7
2.4 BDD Process, Step 2..n
.feature BDD Application
Source Code Source Code
Native Executable Executable
language test code application code
executable complies
specification
.feature BDD Application
Source Code Source Code
Native Executable Executable
language test code application code
executable complies
specification
2015-03-28 - 2015-03-29 BDD Hands-on with Python 8
3.1 GUI Design Process - Tools# GUI Designer The generated code
Name Platform GUI Framwork
Screen Layout
Class Runtime License Ok?
1 page Tcl/Tk Tkinter Grid Yes Yes
2 ptkgb Python Tkinter Grid Yes Python megawidgets (pmw)
MIT Yes
3 ptkgen Python Tkinter Grid No tkgen GPL 2.1 Yes
4 pygubu Python TkInter Grid Yes pygubu GPL 3 Yes
5 QT Creator Python QT, PyQT Grid Yes PyQT, QT GPL 2 or 3 Yes
6 rapyd Python Tkinter Grid Yes rpErrorHandler.py Public Domain?
Not yet
6 SpecTCL Python Tkinter Grid Yes Yes
7 tk_happy Python Tkinter Place Yes Yes
9 Visual Python Tkinter IDE 1v1.6
.NET Tkinter Place No No
2015-03-28 - 2015-03-29 BDD Hands-on with Python 9
● If the code by GUI designers needs a runtime, what is the runtime's license ( GPL, LGPL, MIT, public domain,..)?
● Efficent naming - When selecting names for GUI designer objects, think of Python naming conventions!
– Class => CamelCode– Class method / function => Lowercase & underscore– Variable
● Global variable => Capitals only● Local variable => Lowercase & underscore
3.2 GUI Design Process - Practice
2015-03-28 - 2015-03-29 BDD Hands-on with Python 10
1. Domain expert of the customer and software developer
● Creation of BDD documents
2. Software developer
➢ Auto-generation of a basic script
– PowerShell.exe -ExecutionPolicy Unrestricted➢ behave my.feature 2>&1 >my.py
➢ Make the script runable ( avoid $FF in the file )
➢ Implement a step
➢ Implement steps with variables
➢ Implement steps with regular expressions
➢ Test-driven Implementation of the application, using PyUnit
4 Coding...
2015-03-28 - 2015-03-29 BDD Hands-on with Python 11
3 Domain expert of the customer and software developer
● Integration test of the application
● Presentation and roll-out of the application
● Project feedback
4 Coding...
2015-03-28 - 2015-03-29 BDD Hands-on with Python 12
● Most GUI designers create code which
● Doesn´t complie with PEP8...
● Isn't passed by codecheckers ( PyLint.. ) without much warnings...
● Often doesn´t meet the Python naming conventions,
– By the tool
– Or by user's wrong choice when using the tool!● Love it or leave it!
● If you must complete the generated code files, manually
– If you know that you will or can never use the GUI designer again, then you might once modify the code to make it PEP8 / PyLint compliant
● Else ( PyQT => Class derivation ) .. accept the code as it is
● Yet another project for the GUI designer: Modify the code generator... :-)
5 PEP8 Compliance
2015-03-28 - 2015-03-29 BDD Hands-on with Python 13
<dev_root>codebreaker.py
<dev_root>/Scripts/codebreaker-script.py
<dev_root>/project/d_implementation/steps => <inst_root><codebreaker_package>
Setup.py
from distutils.core import setup
setup(name='codebreaker',
version='1.0',
data_files=[('Lib/site-packages/codebreaker_package', ['AUTHORS', 'COPYING', 'README.txt']),
('Scripts', ['Scripts/codebreaker.bat', 'Scripts/codebreaker.sh']) ],
scripts=['Scripts/codebreaker-script.py'],
py_modules=['Scripts/codebreaker-script', 'codebreaker'],
package_dir={'codebreaker_package': 'project/d_implementation/steps'},
packages=['codebreaker_package'])
6.1 Setup by distutils
2015-03-28 - 2015-03-29 BDD Hands-on with Python 14
cd /<dev_root>
setup sdist
REM "<dev_root>/dist/codebreaker-1.0.zip" is created
cd dist
unzip codebreaker-1.0.zip
setup install
REM The directory "<dev_root>/build" is created
cd /<dev_root>
setup.py bdist_wininst
REM "<dev_root>/dist/codebreaker-1.0.win32.exe" is created
6.2.1 Installation by setup.py
2015-03-28 - 2015-03-29 BDD Hands-on with Python 15
setup install
c:\Python27\Lib\
c:\Python27\Lib\side-packages
c:\Python27\Scripts
6.2.2 Installation by setup.py
2015-03-28 - 2015-03-29 BDD Hands-on with Python 16
virtualenv c:\Users\Public\python
setup.py install –prefix="c:\Users\Public\python"
c:\Users\Public\python\Lib\site-packages
c:\Users\Public\python\Lib\site-packages\codebreaker_package
c:\Users\Public\python\Lib\site-packages\Scripts
c:\Users\Public\python\Script
set_pythonhome.bat
set path=%path%;c:\users\public\python\Scripts
set pythonhome=c:\users\public\python
set_pythonpath.bat
set path=%path%;c:\users\public\python\Scripts
set pythonpath=c:\users\public\python\Scripts;c:\users\public\python\Lib;c:\users\public\python\Lib\site-packages
6.2.3 Installation by setup.py
2015-03-28 - 2015-03-29 BDD Hands-on with Python 17
REM Test
cd \users\public
codebreaker
codebreaker -i page
REM Correction
cd c:\Python27\Scripts
ren pygubu.pyw pygubu-script.py
copy con pygubu.bat
Pygubu-script.py
^z
6.3 Test & Correction
2015-03-28 - 2015-03-29 BDD Hands-on with Python 18
C:\Users>codebreaker.bat
C:\Users>codebreaker-script.py
Running Python sys.version_info(major=2, minor=7, micro=6, releaselevel='final',
serial=0) on 'win32'
Traceback (most recent call last):
File "C:\Python27\Scripts\codebreaker-script.py", line 31, in <module>
import codebreaker
File "C:\Python27\lib\site-packages\codebreaker.py", line 31, in <module>
from codebreaker_package.cb import main as cb_main
File "C:\Python27\lib\site-packages\codebreaker_package\cb.py", line 36, in <m
odule>
from pygubu_codebreaker import main as pygubu_main
File "C:\Python27\lib\site-packages\codebreaker_package\pygubu_codebreaker.py"
, line 33, in <module>
import pygubu
File "C:\Python27\Scripts\pygubu.pyw", line 43, in <module>
print("Pygubu: v. %s" % (pygubu.__version__,))
AttributeError: 'module' object has no attribute '__version__'
6.4 Pygubu Module - Problem
File "C:\Python27\Scripts\pygubu.pyw", line 43, in <module>
2015-03-28 - 2015-03-29 BDD Hands-on with Python 19
.gitignore
.pypirc
*.zip
*.exe
*.rpm
build
bin
dist
6.5 Publication on GitHub
2015-03-28 - 2015-03-29 BDD Hands-on with Python 20
7 Project Feedback
● If BDD is intended to describe business logic, what to do with UI messages?
● BDD does not help to describe business algorithms ( in opposite to UML, BPMN )
● Tricky "Scenarios: matches with duplicates"
● TDD & Unittests => helpful
● I felt helpless with BDD
● Behave produces much output garbage
● Integration with IDEs, with information filtering would be helpful
● Setup implementation
● revealed errorous naming of script file of a dependant module ( Pygubu )
● Distutils doesnt' handle requirements ( requires=["required_module"] ) as promised
2015-03-28 - 2015-03-29 BDD Hands-on with Python 21
8 Roadmap
● Webserver-based versions ( Bottle, CherryPy, Django, Genshi, Gunicorn, Plone/Zope, Pocco "Flask", Pylons Project "Pyramid", Python Paste, Tornado )
● Further GUI-based versions ( Kivy, wxPython, PyGTK.. ),
● Online-service based versions ( Heroku, PythonAnywhere,..)
● Module for the ERP systems Tryton / OpenERP
● Implementation for other installers ( i.e. Setuptools / Easy Install )
2015-03-28 - 2015-03-29 BDD Hands-on with Python 22
9 Resources
● Book
● David Chelimsky, Dave Astels, Bryan Helmkamp, Dan North, Zach Dennis, Aslak Hellesoy: "The RSpec Book: Behaviour Driven Development with RSpec, Cucumber, and Friends", ISBN 1934356379
● Rolf Hemmerling's webpage, also about BDD http://www.hemmerling.com/doku.php/en/specbyex.html
● Rolf Hemmerling's webpage, also about GUI designers http://www.hemmerling.com/doku.php/en/python.html
● Rolf Hemmerling's Slides http://www.slideshare.net/hemmerling/
Top Related