You are on page 1of 144

Test Automation In Python

Kyle McEvoy
Director of QA

@_kmce
First conf talk! Thanks NTD!
Kyle McEvoy
Director of QA


@_kmce
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!

skills
Your
Why Python? Why not X?

● No language is truly ‘better’ overall


● It’s all about context!
Skill
s of
team
skills
Your
Why Python? Why not X?

● No language is truly ‘better’ overall


● It’s all about context!
o -location
Skill Test c
s of
team
skills
Your
Why Python? Why not X?

● No language is truly ‘better’ overall


● It’s all about context!
o -location
Skill Test c
s of
team
skills
Your
le p ack a ges
Availab
Why Python? Why not X?

● No language is truly ‘better’ overall


● It’s all about context!
o -locatio n
Skill Test c
s of
team
skills Externa
l suppo
Your rt
le p ack a ges
Availab
Why Python? Why not X?

● No language is truly ‘better’ overall


● It’s all about context!
o -loca tio n
Skill Test c
s of
team
skills Externa
l suppo
Your rt
le p ack a ges Design princ
Availab iples
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

● Lower barrier for entry


● Open source
● Scripted language
● Platform independent
● Extensive standard library
● Extensive community packages (PyPI)
● Vibrant and supportive community
Java

Python
Java

Python
1<5
s e rs
B ro w
s e rs
B ro w
N a tiv e
Apps
s e rs
B ro w
N a tiv e
Apps

A P Is
s e rs
B ro w
N a tiv e
Apps

A P Is
u t io n /C o n c u r r e n cy
Distrib
BDD
s e rs
B ro w
N a tiv e
Apps

A P Is
u t io n /C o n c u r r e n cy
Distrib
BDD
s e rs
B ro w
N a tiv e n g
Apps R e p o r t i

A P Is
u t io n /C o n c u r r e n cy
Distrib
Test Framework
BDD
s e rs
B ro w
N a tiv e n g
Apps R e p o r t i

A P Is
u t io n /C o n c u r r e n cy
Distrib
Popular test frameworks

● unittest (standard library)


Popular test frameworks

● 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
Basic JUnit Test
FirstJUnit5Tests.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 pytest
Terminal
Installing pytest
Terminal

Pip Installs Packages


Installing pytest

You PyPI
Installing pytest

You PyPI
Installing pytest
Terminal

Virtual Environments
Python Virtual Environments
Project1 Project2 Project3

venv venv2 venv3


- pytest (3.2.2) - pytest (3.2.2) - pytest (3.5.1)
- custom (1.3.1) - custom (2.0.1)
Python Virtual Environments
WebTests IOSTests APITests

wtests ios apienv


- pytest (3.2.2) - pytest (3.2.2) - pytest (3.5.1)
- custom (1.3.1) - custom (2.0.1)
Basic pytest Test

test_file_example.py
Test result
Terminal
Basic pytest Test

test_file_example.py
Terminal Test result
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 TEARDOW N
Test Function

SETUP EXERCISE VERIFY TEARDOW N


Test Fixtures

SETUP EXERCISE VERIFY TEARDOW N


JUnit fixture example
FirstJUnit5Tests.java
JUnit fixture example
FirstJUnit5Tests.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 API Test
Methods
● Login
● Get my username
● Give money
● Logout
Use case API Test
Methods
● Login
● Get my username
● Give money
● Logout

Methods under test


● Get my username
● Give money
Use case API Test
Methods
● Login
● Get my username
Test Setup
● Give money ● Login
● Logout

Methods under test


● Get my username
● Give money
Use case API Test
Methods
● Login
● Get my username
Test Setup
● Give money ● Login
● Logout

Methods under test Test Teardown


● Get my username ● Logout
● Give money
Use case API Test
Methods
● Login
● Get my username
Test Setup Already created
● Give money ● Login
● Logout ● API Client SDK
● Data models
(e.g. User)
Methods under test Test Teardown ● Test user
● Get my username ● Logout
● Give money
test_file_example.py
test_file_example.py
test_file_example.py
fixtures
test_file_example.py
fixtures
test
test_file_example.py

imports
fixtures
test
conftest.py

test_usernames.py
conftest.py

test_usernames.py test_credit.py
Use case API Test
Methods
● Login
● Get my username
Test Setup Already created
● Give money ● Login
● Logout ● API Client SDK
● Data models
(e.g. User)
Methods under test Test Teardown ● Test user
● Get my username ● Logout
● Give money
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

test_file_example.py

Terminal
test_file_example.py
*n o t a re a l tw e e t
Use case
Use case Browser Test
Use case Browser Test

Page under test


● Deposit page
Use case Browser Test

Page under test Test Setup


● Deposit page ● Create user
● Load browser
● Login as user
● Navigate to
Deposit Page
Use case Browser Test

Page under test Test Setup Test Teardown


● Deposit page ● Create user ● Close browser
● Load browser
● Login as user
● Navigate to
Deposit Page
Test Setup
● Create user
● Load browser
● Login as user
● Navigate to
Deposit Page

Test Teardown
● Close browser
Test Setup
● Create user user fixture
● Load browser
● Login as user
● Navigate to
Deposit Page

Test Teardown
● Close browser
Test Setup
● Create user user fixture
● Load browser selenium fixture
● Login as user
● Navigate to
Deposit Page

Test Teardown
● Close browser
Test Setup
● Create user user fixture
● Load browser selenium fixture
● Login as user logged_in_browser fixture
● Navigate to
Deposit Page

Test Teardown
● Close browser
Test Setup
● Create user user fixture
● Load browser selenium fixture
● Login as user logged_in_browser fixture
● Navigate to
deposit_page fixture
Deposit Page

Test Teardown
● Close browser
Test Setup
● Create user user fixture
● Load browser selenium fixture
● Login as user logged_in_browser fixture
● Navigate to
deposit_page fixture
Deposit Page

Test Teardown
● Close browser 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 c o n te s ts
us er s
s & mo
Common s e r v ic e re

Fixtures & helpers re w ar ds e n tr ie s


Further reading
pytest.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

You might also like