Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development...

24
Test and Behavior Driven Development with Python 1 Lorena Lobato Pardavila, IT-CM-LCS 2nd Developers@CERN Forum CERN, 31 st May 2016

Transcript of Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development...

Page 1: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

Test and Behavior Driven

Development with Python

1

Lorena Lobato Pardavila, IT-CM-LCS

2nd Developers@CERN Forum

CERN, 31st May 2016

Page 2: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

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?

Page 3: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

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

Page 4: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

4

4

2nd Developers@CERN Forum

What is Test-Driven Development?

Page 5: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

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

Page 6: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

6

6

2nd Developers@CERN Forum

Is Behaviour-Driven Development

the same?

Page 7: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

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)

Page 8: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

8

8

2nd Developers@CERN Forum

Behaviour-Driven Development (BDD)

Page 9: Test and Behaviour Driven Development with Python€¦ · 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

Page 10: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

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

Page 11: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

11

11

2nd Developers@CERN Forum

Behave: Environment Controls

Page 12: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

12

12

2nd Developers@CERN Forum

Behave: Write the feature test

Page 13: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

13

13

2nd Developers@CERN Forum

Behave: Provide the Test Automation

Page 14: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

14

14

2nd Developers@CERN Forum

Behave: Run your test

Page 15: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

15

15

2nd Developers@CERN Forum

Behave example: DNS LB

Page 16: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

16

16

2nd Developers@CERN Forum

Behave example: DNS LB

Page 17: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

17

17

2nd Developers@CERN Forum

What if…?

Page 18: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

• 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

Page 19: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

27/05/16

19

19

2nd Developers@CERN Forum

Selenium Web Driver

Page 20: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

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

Page 21: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

• 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..

Page 22: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

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/

Page 23: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

Questions?

THANKS FOR YOUR ATTENTION!

27/05/16 tCSC 2016: "Efficient and

Parallel Processing of

23

CONTACT

Work: [email protected]

Twitter: @lobatopardavila

Page 24: Test and Behaviour Driven Development with Python€¦ · Test BDD TDD Behaviour-Driven Development (BDD) 27/05/16 8 8 2nd Developers@CERN Forum Behaviour-Driven Development (BDD)

24