Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend pytest.approx to handle strings. #3482

Closed
Drew-Ack opened this issue May 16, 2018 · 12 comments
Closed

Extend pytest.approx to handle strings. #3482

Drew-Ack opened this issue May 16, 2018 · 12 comments
Labels
topic: approx related to pytest.approx function type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@Drew-Ack
Copy link

This issue is a developer and community discussion on how strings should be handled in pytest.approx()

Two of the easiest ways to do text matching in pytest are:

  1. Regex Matching
  2. assert "abc" in "abcdef"

I enjoy the benefits that using assert gives me because of its strength in pytest, but It's not as flexible as Regex could possibly be.

A handy use case I could see is checking to see if an expected string is close to a "template"

psudo-code###

template = "You have saved {%} record"
returned_string = "You have saved 10 records"
approx(returned_string, template) -> true

perhaps there is a bounds of some sort

template = "You have saved {%} record"
returned_string = "You have saved 7 records"
bounds = [0, 10]
approx(returned_string, template, bounds) -> true

bounds = [0,5]
approx(returned_string, template, bounds) -> false

It seems that a template/bounds combo seems to be a good way to extend string comparison beyond the substring in string model.

I am very eager to think about other ideas or use cases people have 😄

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #3085 (Remove support for strings to pytest.main), #719 (pytest.mark.parametrize string-based parameter list doesn't handle single element tuples), #109 (Unable to extend reports when using pytest-xdist), #1411 (Pytest stops), and #544 (pytest 2.6 remove some strings in pathname).

@RonnyPfannschmidt
Copy link
Member

i propose using a new name for comp arables that support string matching, as its strictly a different topic

@Drew-Ack
Copy link
Author

If I understand correctly, we make a new class that is for comparing objects. Leaving approx alone so it can focus on numbers.

Should this new comparable class focus strictly on strings or be constructed in such a way that it can expand to other types?

@RonnyPfannschmidt
Copy link
Member

this should be a plugin as a starting point as its not clear how to name things and how it should work

@nicoddemus
Copy link
Member

I agree on using a different name than approx for this, besides the potential for confusion I see the "text matching" class using different configuration options on how to do the matching.

One pattern that I've used in the past is something like this:

assert text == pytest.text_diff(expected, diff_lines=[25, 26, 78])

Where diff_lines would specify lines where the texts could differ: this is common for generated text which includes check sums or date of generation.

OTOH I've not used the approach anymore because it is hard to maintain the text along the code (specially large texts) so lately I have been using an approach similar to pytest-regtest.

@nicoddemus nicoddemus added the type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature label May 16, 2018
@RonnyPfannschmidt
Copy link
Member

again - this should start outside of pytest, and have much more room for experimentation

@Drew-Ack
Copy link
Author

Im fine with starting it as a plugin and going from there.
I believe this would be a good place continue discussing functionality of said plugin

@marcelotrevisani
Copy link
Contributor

huumm... interesting, do you know if someone already started to create this plugin?

@RonnyPfannschmidt
Copy link
Member

At least I am unaware

@marcelotrevisani
Copy link
Contributor

I think I will try to develop a plugin like that. It does not seem to be that difficult, let's see
:p

@nicoddemus
Copy link
Member

@marcelotrevisani cool, let me know if you have any questions.

Also please update this issue if you end up publishing the plugin. 👍

@nicoddemus
Copy link
Member

I think we have a consensus that this should at least start as a plugin. Can close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: approx related to pytest.approx function type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

5 participants