test and behaviour driven development with python€¦ · test bdd tdd behaviour-driven development...
TRANSCRIPT
Test and Behavior Driven
Development with Python
1
Lorena Lobato Pardavila, IT-CM-LCS
2nd Developers@CERN Forum
CERN, 31st May 2016
27/05/16
2nd Developers@CERN Forum 2
2
What I am going to talk?
• What do you know about testing?
• What is Test-Driven Development?
• Is Behaviour-Driven Development the same?
• Unit Testing tools: Behave and Selenium Web Driver
• What do we take from this talk?
27/05/16
3
3
2nd Developers@CERN Forum
What do you know about Testing?
White-Box Testing Focus on the activities. You know the
source of the program
Black-Box Testing Focus in the result. You have no idea how
the program should work
Unittest Single piece of code to be tested
Integration testing Multiple pieces are tested together
Acceptance Testing Automatic testing of the entire application
TDD vs BDD Agile Software Development techniques
27/05/16
4
4
2nd Developers@CERN Forum
What is Test-Driven Development?
27/05/16
5
5
2nd Developers@CERN Forum
Test-Driven Development (TDD)
Add test
Refactor Run tests
Does the
test fail?
Write the
code
27/05/16
6
6
2nd Developers@CERN Forum
Is Behaviour-Driven Development
the same?
27/05/16
7
7
2nd Developers@CERN Forum
Write a
Failing
Unit Test
Make the
Test Pass
Refactor
Write a Failing
Acceptance
Test
BDD TDD
Behaviour-Driven Development (BDD)
27/05/16
8
8
2nd Developers@CERN Forum
Behaviour-Driven Development (BDD)
27/05/16
9
9
2nd Developers@CERN Forum
UNIT TESTING TOOLS
BDD framework based on Python implementation
of the Cucumber specification
Behave uses tests written in a natural language
style, backed up by Python code.
BDD concepts: When, Given, Then
27/05/16
10
10
2nd Developers@CERN Forum
Behave
27/05/16
11
11
2nd Developers@CERN Forum
Behave: Environment Controls
27/05/16
12
12
2nd Developers@CERN Forum
Behave: Write the feature test
27/05/16
13
13
2nd Developers@CERN Forum
Behave: Provide the Test Automation
27/05/16
14
14
2nd Developers@CERN Forum
Behave: Run your test
27/05/16
15
15
2nd Developers@CERN Forum
Behave example: DNS LB
27/05/16
16
16
2nd Developers@CERN Forum
Behave example: DNS LB
27/05/16
17
17
2nd Developers@CERN Forum
What if…?
• Open source, web-based testing automation tool
• It aims to mimic the behaviour of a real user, and as such
interacts with the HTML of the application.
• Muti-language backend support (Java, Ruby, Python, C#, PHP…)
• Supports Cross-Browser Testing. The tests can be run on multiple
browsers.
27/05/16
18
18
2nd Developers@CERN Forum
Selenium Web Driver
27/05/16
19
19
2nd Developers@CERN Forum
Selenium Web Driver
27/05/16
202nd Developers@CERN Forum
Selenium Web Driver Example: DNS LBfrom selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import unittest
class NewvisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
pass
def test_sso_login(self):
self.browser.get("https://aiermis.cern.ch/lbweb")
self.assertIn('DNS Load Balancing', self.browser.title)
link = self.browser.find_element_by_link_text('CERN SSO Login') .click()
self.assertIn('Cern Authentication', self.browser.title)
link = self.browser.find_element_by_link_text('Sign in using your current Windows/Kerberos credentials').click()
page_text = self.browser.find_element_by_tag_name('body').text
self.assertIn('Logged in', page_text)
inputbox = self.browser.find_element_by_id('id_new_alias')
inputbox.send_keys(‘hola-developers!')
inputbox.send_keys(Keys.ENTER)
…........
if __name__ == '__main__':
unittest.main()
• Unit Testing gives you the what. Test-Driven
Development gives you the when. Behavior Driven-
Development gives you the how.
• Run tests often by integrating these tools with CI
systems.
• Behave is great to have the code documented and to
know in advance the requirements
• Selenium Web Driver for “lazy people”
27/05/16
21
21
2nd Developers@CERN Forum
From this talk..
I like it! Where can I start from?
27/05/16
22
22
2nd Developers@CERN Forum
Unit testing Python framework https://docs.python.org/2.7/library/unittest.html
General rules Python testing http://docs.python-guide.org/en/latest/writing/tests/
Behave http://pythonhosted.org/behave/
Selenium Web Driver http://selenium-python.readthedocs.io/
Test-Driven Development with
Python
http://chimera.labs.oreilly.com/books/1234000000754/
Questions?
THANKS FOR YOUR ATTENTION!
27/05/16 tCSC 2016: "Efficient and
Parallel Processing of
23
CONTACT
Work: [email protected]
Twitter: @lobatopardavila
24