James_E
I see that the current ExUnit source code has support for rich failure messages on a small whitelist of “recognized” assertion patterns, namely:
assert match?(left, right),assert left = rightrefute match?(left, right),refute left = rightassert left === right,refute left !== right,assert left !== right,refute left === right,assert left == right,refute left != right,assert left != right,refute left == right,assert left >= right,refute left < right,assert left > right,refute left <= rightassert left <= right,refute left > rightassert left < right,refute left >= rightassert left ~= rightrefute left ~= right
Would there be any interest in a pull request adding support for an additional pattern:
refute left and not right(implication, right must be true whenever left is)
This pattern is coming up repeatedly as the most robust, developer-intent-communicating form for some unittests I’m writing:
property "delete returns a well-formed multiset whenever input is well-formed" do
check all ms <- t(term(), strict: false),
value <- term(),
count <- one_of([non_negative_integer(), constant(:all)]) do
result = One9.Ms.delete(ms, value, count)
refute One9.Ms.well_formed?(ms) and not One9.Ms.well_formed?(result)
end
end
property "put 0 copies doesn't corrupt struct" do
check all multiset <- t(term()), value <- term() do
result = One9.Multiset.put(multiset, value, 0)
assert One9.Ms.well_formed?(result.counts)
assert One9.Multiset.equals?(result, multiset)
refute One9.Multiset.member?(result, value) and not One9.Multiset.member?(multiset, value)
end
end
And having these tests augmented with a little bit of extra verbiage and awesome debug formatting, like the comparison checks currently have, would be a minor boon.
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 8 of 8 Posts
tfwright
Personally, I think the most important value for test cases is simplicity, so I’m definitely skeptical about support for something like this.
FWIW, about the first example specifically I would argue that you should not be writing assertions against test inputs since they are supposed to be known. Granted I’m not familiar with the DSL shown so maybe I’m misunderstanding something but generally I would only expect the second part of that assertion to be a meaningful test since the former would be testing the test itself.
James_E
This is just property-based testing. You declare what type of inputs your functions shouldn’t choke on, and the unittester fuzzes your code for you, then attempts to make minimum viable repros of any failing examples.
Tangent
I do use “old fashioned hardcoded-input” testing liberally within the doctests, and I have a few exemplar edge or obscure prototype cases hard-coded in other unit tests. But having a per se stream of well-formed spaghetti being thrown against the walls of my code is extremely valuable and I use it for the heavy lifting.
tfwright
I had a feeling that was what I was looking at. Nothing against property-based testing as another tool in the arsenal, although personally I haven’t found need for it as of yet in the types of programs I have tested. But I wouldn’t think ExUnit needs to include special support for it.
But also even property tests, as far as I understand, should be able to control inputs at least in terms of types. If one type of input should raise an error, but another should fail gracefully, you need to be able to write a test that isolates both cases, otherwise the distinction is ipso facto irrelevant and shouldn’t be subject to any test. In your example, if the concern is specifically with “well formed” inputs, then I would argue should be a constraint on what is tested in that example, rather than an assertion on the result.
christhekeele
This seems like a reasonable conversation to start on the core mailing list, if you can’t find a prior discussion.
I think property-based testing and other test design philosophy is orthogonal to this discussion:
James_E
That is correct; they do. The issue arises with situations like those given in my OP.
I’m not entirely sure what you’re getting at, here.
Of course, I can do that (and I do do that, in other tests). But when “A implies B” is exactly what I mean to assert, stuffing some of the conditions for A into the generator headers achieves terseness, at the cost of self-documentingness, which is a very raw deal.
I agree almost entirely—except that I suspect “A implies B” is very unlikely to come up as a natural, relevant, correct thing to test outside of property-based testing (which, I suspect, is what @tfwright has been trying to build an argument for this whole time.)
tfwright
Agree on the first (property-based) if there is another use case for this syntax, but not so much on the latter (design philosophy). To shift focus back to the latter, my opinion is precisely that support for this would encourage test design I would consider suboptimal on the principles of simplicity and atomicity. I just think tests should generally avoid testing more than one thing at once, regardless of whether that “other thing” is considered an input or not (although if it’s an input then I think it’s particularly bad).
James_E
Ironically, the tooling that’s already in there works great for the strictly more complicated case, “A implies B, and not-A implies not-B”—since
===works for booleans.I file B as
left, A asright, and I get an extremely educational error message when things break down that colors A green and B red if they ever disagree:^That’s what got me on the train of thought of pulling out just the first half for assertions.
fuelen
When guards are failing, boolean logic is highlighted with colours. I don’t see any reason why it shouldn’t be supported in ExUnit as well.
As @christhekeele has already suggested, please start a discussion on the core mailing list.