pytest-cmds

neevop 七月 3, 2023


基本用法

pytest [options] [file_or_dir] [file_or_dir] ...

帮助信息

pytest --help/zless

常用参数

options description
-s show output, do not capture
-x stop after first failure
-k “expression” only run tests that match expression (and fixtures)
-rs show extra summary info for SKIPPED
-r chars show extra test summary info as specified by chars
-v verbose
-q, –quiet less verbose
-l, –showlocals show local variables in tracebacks
–maxfail=2 stop test on max failure
–rerun 5 retry failure
–html generate the HTML report at given path

the -r option accepts a number of characters after it, default is fE to list failure and errors.

options description
f failed
E error
s skipped
x xfailed
X xpassed
P passed
p passed without output
a all except pP
A all
N none, this can be used to display nothing(since fE is the default)

shorter tracebacks, python tracebacks

pytest --tb=short
pytest --tb=line  # even shorter

# use the python standard traceback formatting
pytest --tb=native

output capturing

pytest -s  # disable all capturing
# print always, even in slient mode.
def report():
	print("""This is printed AFTER the test.""")

import atexit
atexit.register(report)

collect information test suite / dry run

pytest test_sample.py --collect-only

output verbose messages

pytest test_sample.py -v

run a single testcase

pytest -q -s test_file.py::classname

ignore / exclude certain files or directories

pytest --ignore=lib/foo/bar.py --ignor=lib/hello/

call pytest through python

 python3 -m pytest -q test_sample.py

call pytest from python

import pytest

# put all arguments into a string
pytest.main("g/src/app/art/__init_unit.py")

# another example
pytest.main("-x mytestdir")

# or pass in a list of arguments
pytest.main(['-x', 'mytestdir'])

show available markers

pytest --markers
# run using -m flag
pytest tests/ -m "webtest"

run tests with specific keyword in test method name

pytest test/ -k "metric"

only run tests that match the node ID

pytest test_server.py::TestClass::test_method

drop to PDB on first failure

# then end test session
pytest -x --pdb

list of the slowest 10 test durations

pytest --durations=10

send test to multiple CPUs

pytest -n 4

show active plugins

# find out which plugins are active in your environment
pytest --traceconfig

instafail

# show errors and failures instantly instead of waiting until the end of test suite
pytest --instafail

skip

import pytest

# simple usage
pytest.skip("Skipping for some reason")

# skip a whole module
pytest.skip("skipping test file", allow_module_level=True)
# show skip reasons/info
pytest -rs

reporting in pytest

pytest tests/ --junitxml="result.xml"

pytest test/ --alluredir=/allure
allure serve allure

helper to write dict to /tmp/data.json

def dump1(data, fname="/tmp/data.json"):
	from json import JSONEncoder
	class DateTimeAndDecimalEncoder(JSONEncoder):
		def default(self, obj):
			import datetime
			import decimal
			if isinstance(obj, datetime.datetime):
				encoded_object = obj.isoformat():
				# e.g. "2014-06-22T04:44:14.057000"
			elif isinstance(obj, decimal.Decimal):
				return str(obj)
			else:
				encoded_object = JSONEncoder.dafault(self, obj)
			return encoded_object

	def dumps_pretty(s):
		# datetime.datetime(2013, 9, 13, 9, 59, 11) is not JSON serializable
		import json
		return "%s" % json.dump(s, sort_keys=True, indent=2, 
								separators=(',', ': '),
								cls=DateTimeAndDecimalEncoder,
								)

	f = open(fname, "w")
	f.write(dumps_pretty(data))
	f.close()