Last active
August 29, 2015 14:03
-
-
Save blueyed/88481f40138020bab62d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: http://pytest.org/dev/example/simple.html#making-test-result-information-available-in-fixtures | |
@pytest.mark.tryfirst | |
def pytest_runtest_makereport(item, call, __multicall__): | |
# execute all other hooks to obtain the report object | |
rep = __multicall__.execute() | |
# set an report attribute for each phase of a call, which can | |
# be "setup", "call", "teardown" | |
setattr(item, "rep_" + rep.when, rep) | |
return rep | |
import junitxml, hashlib | |
@pytest.fixture | |
def screenshot_on_failure(request): | |
def fin(): | |
import ipdb; ipdb.set_trace() | |
# request.node is an "item" because we use the default | |
# "function" scope | |
if request.node.rep_setup.failed: | |
print "setting up a test failed!", request.node.nodeid | |
elif request.node.rep_setup.passed: | |
if request.node.rep_call.failed: | |
print "executing test failed", request.node.nodeid | |
# try: | |
browser = request.getfuncargvalue('browser') | |
# except Exception: | |
# return | |
project_dir = request.getfuncargvalue('project_path') | |
names = junitxml.mangle_testnames(request.node.nodeid.split("::")) | |
classname = '.'.join(names[:-1]) | |
screenshot_dir = os.path.join(project_dir, classname) | |
if not os.path.exists(screenshot_dir): | |
os.makedirs(screenshot_dir) | |
name = hashlib.md5(u'{0}-{1}'.format(names[-1], step.name).encode('utf-8', 'ignore')).hexdigest() | |
screenshot_file_name = '{0}.png'.format(name) | |
screenshot_path = os.path.join(screenshot_dir, screenshot_file_name) | |
browser.driver.save_screenshot(screenshot_path) | |
sys.stderr.write('[[ATTACHMENT|{0}]]'.format(os.path.join( request.config.option.screenshot_path_prefix, classname, screenshot_file_name))) | |
request.addfinalizer(fin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment