Pytest-ing Cheat Sheet

A Programming › Python cheat sheet in English by amicheletti
16,140 views 4,295 downloads 17 Jul 2017 History
3.7 avg · 0 ratings

Pytest and coverage Tool

py.test <fi­les>
Run py.test tool
--maxf­ail=X
Exit after failing X tests
--dura­tio­ns=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

Pytest with Flask

client = app.te­st_­cli­ent()
Gives you an Test Client object
client.ge­t(url)
Makes a GET request
client.po­st(url, data=[])
Makes a POST request
client.pu­t(url, data=[])
Makes a PUT request
You can create a test client to test your Flask app

Coverage Config­uration File

.cover­agerc
Default filename looked for
[report]
Section defining the report
exclud­e_lines =
Exclude lines defined after
Write after exclud­e_lines = the lines as they are in Python code

Fixture

@pytes­t.f­ixt­ure()
Fixture decorator
scope=­"­ses­sio­n"
Scope for the fixture: Can be session, module, class or function
params=[]
For each value, the fixture will be called with that value (this makes multiple calls)
autous­e=False
All tests in the session use the fixture automa­tically
def my_fix­tur­e(r­equest)
Putting request object in fixture gives you access to the pytest request (e.g. put a finalizer)
pytest --fixtures
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.

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 3

Comments (1)

ravishanker2707 08:37 25 Jun 2018

awesome

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.