Test Automation In Python - FINAL · Lessons learned in test automation There are alternatives to...

Preview:

Citation preview

Test Automation In Python

Kyle McEvoy

@_kmce

Director of QA

Kyle McEvoy

@_kmce

Director of QA

First conf talk! Thanks NTD!

We’re hiring!fanduel.com/careers

What Python has to offer for test automation

What Python has to offer for test automation

General use case examples

What Python has to offer for test automation

General use case examples

Lessons learned in test automation

Why Python? Why not X?

Why Python? Why not X?

● No language is truly ‘better’ overall

Why Python? Why not X?

● No language is truly ‘better’ overall● It’s all about context!

Why Python? Why not X?

● No language is truly ‘better’ overall● It’s all about context!

Your skills

Why Python? Why not X?

● No language is truly ‘better’ overall● It’s all about context!

Your skillsSkills of team

Why Python? Why not X?

● No language is truly ‘better’ overall● It’s all about context!

Your skillsSkills of team

Test co-location

Why Python? Why not X?

● No language is truly ‘better’ overall● It’s all about context!

Your skillsSkills of team

Test co-location

Available packages

Why Python? Why not X?

● No language is truly ‘better’ overall● It’s all about context!

Your skillsSkills of team

Test co-location

External support

Available packages

Why Python? Why not X?

● No language is truly ‘better’ overall● It’s all about context!

Your skillsSkills of team

Test co-location

External support

Available packages Design principles

● Lower barrier for entry● Open source● Scripted language● Platform independent● Extensive standard library● Extensive community packages (PyPI)● Vibrant and supportive community

Python

● Lower barrier for entry● Open source● Scripted language● Platform independent● Extensive standard library● Extensive community packages (PyPI)● Vibrant and supportive community

Python

● Lower barrier for entry● Open source● Scripted language● Platform independent● Extensive standard library● Extensive community packages (PyPI)● Vibrant and supportive community

Python

● Lower barrier for entry● Open source● Scripted language● Platform independent● Extensive standard library● Extensive community packages (PyPI)● Vibrant and supportive community

Python

● Lower barrier for entry● Open source● Scripted language● Platform independent● Extensive standard library● Extensive community packages (PyPI)● Vibrant and supportive community

Python

● Lower barrier for entry● Open source● Scripted language● Platform independent● Extensive standard library● Extensive community packages (PyPI)● Vibrant and supportive community

Python

● Lower barrier for entry● Open source● Scripted language● Platform independent● Extensive standard library● Extensive community packages (PyPI)● Vibrant and supportive community

Python

● Lower barrier for entry● Open source● Scripted language● Platform independent● Extensive standard library● Extensive community packages (PyPI)● Vibrant and supportive community

Python

Java

Python

Java

Python1 < 5

Browsers

BrowsersNative Apps

BrowsersNative Apps

APIs

BrowsersNative Apps

APIsDistribution/Concurrency

BrowsersNative Apps

APIsDistribution/Concurrency

BDD

BrowsersNative Apps

APIsDistribution/Concurrency

Reporting

BDD

BrowsersNative Apps

APIsDistribution/Concurrency

Reporting

BDD

Test Framework

Popular test frameworks

● unittest (standard library)

● unittest (standard library)

● Robot Framework

Popular test frameworks

● unittest (standard library)

● Robot Framework

● pytest

Popular test frameworks

● unittest (standard library)

● Robot Framework

● pytest

Popular test frameworks

● unittest (standard library)

● Robot Framework

● pytest

Popular test frameworks

Basic JUnit TestFirstJUnit5Tests.java

Basic pytest Test

test_file_example.py

JUnit

pytest

Basic pytest Test

test_file_example.py

Basic pytest Test

test_file_example.py

Basic pytest Test

test_file_example.py

“test_”

Installing pytestTerminal

Installing pytestTerminal

Pip Installs Packages

Installing pytest

PyPIYou

Installing pytest

PyPIYou

Installing pytestTerminal

Virtual Environments

Python Virtual Environments

venv venv2 venv3- pytest (3.2.2)

- custom (1.3.1)

- pytest (3.2.2)

- custom (2.0.1)

- pytest (3.5.1)

Project1 Project2 Project3

Python Virtual Environments

wtests ios apienv- pytest (3.2.2)

- custom (1.3.1)

- pytest (3.2.2)

- custom (2.0.1)

- pytest (3.5.1)

WebTests IOSTests APITests

Basic pytest Test

test_file_example.py

Test resultTerminal

Basic pytest Test

test_file_example.py

Test resultTerminal

pytest

● Test auto-discovery

pytest

● Test auto-discovery● No boilerplate

pytest

● Test auto-discovery● No boilerplate

● Detailed failure introspection

pytest

● Test auto-discovery● No boilerplate

● Detailed failure introspection

● Simple modular test fixtures

VERIFY

EXERCISE VERIFY

SETUP EXERCISE VERIFY

SETUP EXERCISE VERIFY TEARDOWN

SETUP EXERCISE VERIFY TEARDOWN

Test Function

SETUP EXERCISE VERIFY TEARDOWN

Test Fixtures

JUnit fixture exampleFirstJUnit5Tests.java

JUnit fixture exampleFirstJUnit5Tests.java

pytest fixture example

test_file_example.py

pytest fixture example

test_file_example.py

pytest fixture example

test_file_example.py

Use case

Use case API Test

Use case

Methods● Login● Get my username● Give money● Logout

API Test

Use case

Methods● Login● Get my username● Give money● Logout

Methods under test● Get my username● Give money

API Test

Use case

Methods● Login● Get my username● Give money● Logout

Methods under test● Get my username● Give money

Test Setup● Login

API Test

Use case

Methods● Login● Get my username● Give money● Logout

Methods under test● Get my username● Give money

Test Setup● Login

Test Teardown● Logout

API Test

Use case

Methods● Login● Get my username● Give money● Logout

Methods under test● Get my username● Give money

Test Setup● Login

Test Teardown● Logout

Already created● API Client SDK● Data models

(e.g. User)● Test user

API Test

test_file_example.py

test_file_example.py

test_file_example.pyfixtures

test_file_example.pyfixtures

test

test_file_example.py

fixtures

test

imports

conftest.py

test_usernames.py

conftest.py

test_usernames.py test_credit.py

Use case

Methods● Login● Get my username● Give money● Logout

Methods under test● Get my username● Give money

Test Setup● Login

Test Teardown● Logout

Already created● API Client SDK● Data models

(e.g. User)● Test user

API Test

conftest.py

test_usernames.py test_credit.py

conftest.py

test_usernames.py test_credit.py

conftest.py

test_usernames.py test_credit.py

test_credit.py

test_credit.py

pytest

● Test auto-discovery● No boilerplate

● Detailed failure introspection

● Simple modular test fixtures

pytest

● Test auto-discovery● No boilerplate

● Detailed failure introspection

● Simple modular test fixtures

● Great plugins & complimentary packages

Popular plugins & packages

● Selenium Python bindings (selenium)● pytest Selenium wrapper (pytest-selenium)● Appium Python bindings (Appium-Python-Client)● Behaviour-Driven Development (pytest-bdd)● Test distribution (pytest-xdist)

Popular plugins & packages

● Selenium Python bindings (selenium)● pytest Selenium wrapper (pytest-selenium)● Appium Python bindings (Appium-Python-Client)● Behaviour-Driven Development (pytest-bdd)● Test distribution (pytest-xdist)

Popular plugins & packages

● Selenium Python bindings (selenium)● pytest Selenium wrapper (pytest-selenium)● Appium Python bindings (Appium-Python-Client)● Behaviour-Driven Development (pytest-bdd)● Test distribution (pytest-xdist)

Popular plugins & packages

● Selenium Python bindings (selenium)● pytest Selenium wrapper (pytest-selenium)● Appium Python bindings (Appium-Python-Client)● Behaviour-Driven Development (pytest-bdd)● Test distribution (pytest-xdist)

Popular plugins & packages

● Selenium Python bindings (selenium)● pytest Selenium wrapper (pytest-selenium)● Appium Python bindings (Appium-Python-Client)● Behaviour-Driven Development (pytest-bdd)● Test distribution (pytest-xdist)

Terminal

Terminal

test_file_example.py

test_file_example.py

*not a real tw eet

Use case

Use case Browser Test

Use case Browser Test

Page under test● Deposit page

Use case Browser Test

Page under test● Deposit page

Test Setup● Create user● Load browser● Login as user● Navigate to

Deposit Page

Use case Browser Test

Page under test● Deposit page

Test Setup● Create user● Load browser● Login as user● Navigate to

Deposit Page

Test Teardown● Close browser

Test Setup● Create user● Load browser● Login as user● Navigate to

Deposit Page

Test Teardown● Close browser

Test Setup● Create user● Load browser● Login as user● Navigate to

Deposit Page

Test Teardown● Close browser

user fixture

Test Setup● Create user● Load browser● Login as user● Navigate to

Deposit Page

Test Teardown● Close browser

user fixtureselenium fixture

Test Setup● Create user● Load browser● Login as user● Navigate to

Deposit Page

Test Teardown● Close browser

user fixtureselenium fixturelogged_in_browser fixture

Test Setup● Create user● Load browser● Login as user● Navigate to

Deposit Page

Test Teardown● Close browser

user fixtureselenium fixturelogged_in_browser fixturedeposit_page fixture

Test Setup● Create user● Load browser● Login as user● Navigate to

Deposit Page

Test Teardown● Close browser

user fixtureselenium fixturelogged_in_browser fixturedeposit_page fixture

selenium fixture

pages/deposit_page.py

conftest.py

tests/test_deposit_page.py

tests/test_deposit_page.py

tests/test_deposit_page.py

tests/test_deposit_page.py

decorators.py

decorators.py

conftest.py

tests/test_deposit_page.py

tests/test_deposit_page.py

Client projects

Client projects API projects

Client projects API projects Services Projects

Client projects API projects Services Projects

Client projects API projects Services Projects

Common project

CommonFixtures & helpers

contestsusers

services & more

entriesrewards

Further readingpytest.org (Thank you pytest contributors!)

Lessons learned in test automation

Lessons learned in test automation

● There are alternatives to ‘pre-production’ automation

Lessons learned in test automation

● There are alternatives to ‘pre-production’ automation● Keep it as focused on risk and lightweight as possible

Lessons learned in test automation

● There are alternatives to ‘pre-production’ automation● Keep it as focused on risk and lightweight as possible● It’s a cultural challenge as much as it is technical

Lessons learned in test automation

● There are alternatives to ‘pre-production’ automation● Keep it as focused on risk and lightweight as possible● It’s a cultural challenge as much as it is technical● Testability can often be underrated

Lessons learned in test automation

● There are alternatives to ‘pre-production’ automation● Keep it as focused on risk and lightweight as possible● It’s a cultural challenge as much as it is technical● Testability can often be underrated● Have zero tolerance for test ‘flakiness’

Lessons learned in test automation

● There are alternatives to ‘pre-production’ automation● Keep it as focused on risk and lightweight as possible● It’s a cultural challenge as much as it is technical● Testability can often be underrated● Have zero tolerance for test ‘flakiness’● Peer reviews on tests are invaluable

Thank you!@_kmce

Recommended