How to use longreprtext method in tavern

Best Python code snippet using tavern

conftest.py

Source:conftest.py Github

copy

Full Screen

1from collections import deque2from typing import Any3import pytest4from d8s_file_system import file_read, file_write5from d8s_python import python_function_blocks6from d8s_strings import string_remove_after, string_remove_from_start7FILL_OUTPUT_SIGNAL = 'fill'8FILL_OUTPUT_SIGNAL_IN_CONTEXT = f'== {FILL_OUTPUT_SIGNAL.__repr__()}'9RAISES_OUTPUT_SIGNAL = 'raises'10RAISES_OUTPUT_SIGNAL_IN_CONTEXT = f'== {RAISES_OUTPUT_SIGNAL.__repr__()}'11fill_values = deque()12def pytest_assertrepr_compare(config, op: str, left: object, right: object):13 global fill_values14 if right == FILL_OUTPUT_SIGNAL:15 fill_values.append(left)16 pytest.skip(f'{op} {right.__repr__()}')17def _update_test(file_path: str, function_name: str, new_assertion_value: Any) -> bool:18 """Update the test with the given function_name in the file at the given file_path with the new_assertion_value."""19 test_file_content = file_read(file_path)20 original_function_block = function_block = [21 i for i in python_function_blocks(test_file_content) if function_name in i22 ][0]23 function_block = function_block.replace(FILL_OUTPUT_SIGNAL_IN_CONTEXT, f'== {new_assertion_value.__repr__()}', 1)24 test_file_content = test_file_content.replace(original_function_block, function_block, 1)25 return file_write(file_path, test_file_content)26def _update_test_with_error(file_path: str, function_name: str, error_type: str, erroneous_assertion: str) -> bool:27 """Update the test with the given function_name in the file at the given file_path with the new_assertion_value."""28 test_file_content = file_read(file_path)29 original_function_block = function_block = [30 i for i in python_function_blocks(test_file_content) if function_name in i31 ][0]32 # TODO: currently, the indentation of the new assertion is hard coded; eventually, I would like to get the indentation from the original assertion33 new_assertion = f'with pytest.raises({error_type}):\n {erroneous_assertion}'34 full_erroneous_assertion = f'assert {erroneous_assertion} == \'{RAISES_OUTPUT_SIGNAL}\''35 function_block = function_block.replace(full_erroneous_assertion, new_assertion, 1)36 test_file_content = test_file_content.replace(original_function_block, function_block, 1)37 return file_write(file_path, test_file_content)38def _get_erroneous_call(report_text: str) -> str:39 """."""40 erroneous_line = [41 line for line in report_text.splitlines() if line.startswith('> ') and RAISES_OUTPUT_SIGNAL_IN_CONTEXT in line42 ][0]43 erroneous_assertion = erroneous_line.lstrip('> ')44 erroneous_assertion = string_remove_from_start(erroneous_assertion, 'assert ')45 erroneous_assertion = string_remove_after(erroneous_assertion, ' ==')46 erroneous_assertion = erroneous_assertion.rstrip('= ')47 return erroneous_assertion48@pytest.hookimpl(tryfirst=True, hookwrapper=True)49def pytest_runtest_makereport(item, call):50 global fill_values51 # execute all other hooks to obtain the report object52 outcome = yield53 rep = outcome.get_result()54 # we only look at actual failing test calls, not setup/teardown55 if rep.when == "call" and rep.skipped:56 if fill_values and (FILL_OUTPUT_SIGNAL_IN_CONTEXT in rep.longreprtext):57 file_path = rep.fspath58 function_name = item.name59 new_assertion_value = fill_values.popleft()60 _update_test(file_path, function_name, new_assertion_value)61 elif rep.when == 'call' and rep.failed:62 if RAISES_OUTPUT_SIGNAL_IN_CONTEXT in rep.longreprtext:63 print('here!!!')64 file_path = rep.fspath65 function_name = item.name66 error_type = rep.longreprtext.split(" ")[-1]67 erroneous_assertion = _get_erroneous_call(rep.longreprtext)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tavern automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful