-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Compact way to combine tests that raise with tests that return values #10089
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
Comments
Instead of doing a lot of magic based on the type of the value, you can just parametrize the |
I can really relate to this problem, but I think that having an I think a better approach is the one I outlined in this stack overflow answer. Basically, use either |
#4682 addressed this -- though I really think you said the correct thing out loud and that your tests are trying to be too clever:
this is a good thing |
Got so say, I disagree pretty strongly with the sentiment here. From a usability perspective, covering the core cases in the most compact way possible is extremely valuable. 90% of all unit tests test extremely simple functions across small domains which include one or two error conditions. It is not a problem to be limited in this way, because there is no requirement to use convenience functions, if you want to do more complex tests you can always revert to writing things the long way, but that is usually a minority of cases. When projects have tens of thousands of unit tests then even small advantages in brevity and readability add up. The stack overflow answer you link to seems far less readable than the above proposal, argument unpacking inside the test tuples is not great. I appreciate that from pytest's perspective you probably don't want to tie yourself to dependencies on numpy, pandas etc, personally I work a lot on the scientific stack so most functions return those types. The context manger @The-Compiler mentioned is useful for when you want to test many different exceptions cleanly, and I did not know/consider that approach, so thanks. Anyway, its clear I have wandered into a very old argument so thank you for your time and for maintaining a great project. Its hardly a burden to write things like the above into specific projects where they are useful anyway. It might make more sense as a mini-library for people who test mainly scientific of numerical code. |
@phil20686 i can understand the sentinent for compactness, but its important to keep some domains separate for better understanding the functions you propose cover a wide range of domains that an individual test writer may not even care about, i intent to get together with @asottile in an effort to provide better data matchers and that hopefully will help to trim down those needs while also helping people to come up with better input/expectation tables that being said, its still valuable to be able to compactly express the intents when it comes to data expectations my own subjective impression is, that the example helper you showed makes it very easy to err into the direction of deceptive convenience, and that tends to bite very painful |
What's the problem this feature will solve?
Improved useability in simple cases
Describe the solution you'd like
See code sample and attached examples
Additional context
This is some code that I have written again and again and it removes a lot of boilerplate in writing tests. Seems like it would be a useful addition at source in pytest.
And then you can use it like this:
This code now covers a wide variety of easy cases and leads to extremely compact test cases. To summarise the behaviour of assert call is as follows:
Moreover the equality operator will check for pandas and numpy types, and delegates to their standard equality methods depending on type, allowing you to pass "test_kwargs" such as atol or rtol to the numeric data types.
Without writing functions like this, you end up either having to separate tests that cause errors from tests that do not, or you must put branching logic separately in each test. In my experience the construction above allows you to write much simpler cleaner tests where 90+% of your library unit tests will be a call to assert_call.
The text was updated successfully, but these errors were encountered: