PY-27267: swap diff in pytest (Sync tcmessages and add test)

Actual and expected are in wrong order.
Fixed on tcmessages side, test added.

Use swapdiff in pytest.ini to switch it back
This commit is contained in:
Ilya.Kazakevich
2018-08-24 17:56:25 +03:00
parent 9df6a97ae0
commit d48ce2af51
3 changed files with 40 additions and 5 deletions
@@ -25,7 +25,7 @@ from teamcity import diff_tools
diff_tools.patch_unittest_diff()
def fetch_diff_error_from_message(err_message):
def fetch_diff_error_from_message(err_message, swap_diff):
line_with_diff = None
diff_error_message = None
lines = err_message.split("\n")
@@ -41,7 +41,11 @@ def fetch_diff_error_from_message(err_message):
parts = [x.strip() for x in line_with_diff.split("==")]
parts = [s[1:-1] if s.startswith("'") or s.startswith('"') else s for s in parts]
# Pytest cuts too long lines, no need to check is_too_big
return diff_tools.EqualsAssertionError(parts[0], parts[1], diff_error_message)
expected, actual = parts[1], parts[0]
if swap_diff:
expected, actual = actual, expected
return diff_tools.EqualsAssertionError(expected, actual, diff_error_message)
else:
return None
@@ -71,6 +75,7 @@ def pytest_addoption(parser):
kwargs.update({"type": "bool"})
parser.addini("skippassedoutput", **kwargs)
parser.addini("swapdiff", **kwargs)
def pytest_configure(config):
@@ -89,7 +94,8 @@ def pytest_configure(config):
config._teamcityReporting = EchoTeamCityMessages(
output_capture_enabled,
coverage_controller,
skip_passed_output
skip_passed_output,
bool(config.getini('swapdiff'))
)
config.pluginmanager.register(config._teamcityReporting)
@@ -110,7 +116,7 @@ def _get_coverage_controller(config):
class EchoTeamCityMessages(object):
def __init__(self, output_capture_enabled, coverage_controller, skip_passed_output):
def __init__(self, output_capture_enabled, coverage_controller, skip_passed_output, swap_diff):
self.coverage_controller = coverage_controller
self.output_capture_enabled = output_capture_enabled
self.skip_passed_output = skip_passed_output
@@ -120,6 +126,7 @@ class EchoTeamCityMessages(object):
self.max_reported_output_size = 1 * 1024 * 1024
self.reported_output_chunk_size = 50000
self.swap_diff = swap_diff
def get_id_from_location(self, location):
if type(location) is not tuple or len(location) != 3 or not hasattr(location[2], "startswith"):
@@ -258,7 +265,7 @@ class EchoTeamCityMessages(object):
if err_message.startswith("assert"):
err_message = "AssertionError: " + err_message
if err_message.startswith("AssertionError:"):
diff_error = fetch_diff_error_from_message(err_message)
diff_error = fetch_diff_error_from_message(err_message, self.swap_diff)
except Exception:
pass
@@ -0,0 +1,2 @@
def test_diff():
assert "actual" == "expected"
@@ -66,6 +66,32 @@ public final class PythonPyTestingTest extends PyEnvTestCase {
});
}
@Test
public void testDiff() {
runPythonTest(
new PyProcessWithConsoleTestTask<PyTestTestProcessRunner>("/testRunner/env/pytest/diff", SdkCreationType.EMPTY_SDK) {
@NotNull
@Override
protected PyTestTestProcessRunner createProcessRunner() {
return new PyTestTestProcessRunner("test_diff.py", 1);
}
@Override
protected void checkTestResults(@NotNull final PyTestTestProcessRunner runner,
@NotNull final String stdout,
@NotNull final String stderr,
@NotNull final String all) {
final String expectedConsoleText = "Expected :expected\n" +
"Actual :actual\n" +
" <Click to see difference>";
Assert.assertThat("No diff", runner.getAllConsoleText(), Matchers.containsString(expectedConsoleText));
}
});
}
/**
* Provides existing .xml and checks that configuration is able to parse it
*/