Show Menu

Pytest-ing Cheat Sheet by

python     test     testing     unit

Pytest and coverage Tool

py.test <fi­les­>
Run py.test tool
--max­fail=X
Exit after failing X tests
--dur­ati­ons=10
Use this to Profile your tests (time taken)
--cov­=path
Run coverage within the project path
--cov­-report term-m­issing
Add this to show which lines were not covered
Packages to install:
pip install pytest
pip install pytest­-sugar
pip install pytest­-cov

Coverage Config­uration File

.cove­ragerc
Default filename looked for
[report]
Section defining the report
exclu­de_­lines =
Exclude lines defined after
Write after exclu­de_­lines = the lines as they are in Python code

Pytest with Flask

client = app.te­st_­cli­ent()
Gives you an Test Client object
clien­t.g­et(­url)
Makes a GET request
clien­t.p­ost­(url, data=[])
Makes a POST request
clien­t.p­ut(url, data=[])
Makes a PUT request
You can create a test client to test your Flask app
 

Fixture

@pyte­st.f­ix­ture()
Fixture decorator
scope­="se­ssi­on"
Scope for the fixture: Can be session, module, class or function
param­s=[]
For each value, the fixture will be called with that value (this makes multiple calls)
autou­se=­False
All tests in the session use the fixture automa­tically
def my_fix­tur­e(r­equ­est)
Putting request object in fixture gives you access to the pytest request (e.g. put a final­izer)
pytest --fixt­ures
See all available fixtures
The purpose of test fixtures is to provide a fixed baseline upon which tests can reliably and repeatedly execute.

pytest­-mock

 
In short, mocking is creating objects that simulate the behaviour of real objects.

Download the Pytest-ing Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/amicheletti_pytest-ing.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

Like this cheat sheet? Check out our sponsors!

Readable.io is a collection of tools to make your writing better. More readable content means higher conversion rates and better reader engagement. Measure website and document readability, measure keyword density and more!

Click Here To Get Started!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          MSTest Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet

          More Cheat Sheets by amicheletti