gleb
Assert_value - ExUnit's assert on steroids that writes and updates tests for you
Hi everyone,
We are releasing assert_value. assert_value is ExUnit’s assert on steroids that writes and updates tests for you.
You can use assert_value instead of ExUnit’s assert. It makes Elixir tests interactive and lets you create and update expected values with a single key press.
Here is a simple example. Start with a broken test:
assert_value "foo\n" == """
bar
"""
Run tests as usual. assert_value will show the diff and ask you what to do. Here we like the new value and tell assert_value to accept it:
~/> mix test
test/my_test.exs:6:"test example" assert_value "foo\n" == "bar\n" failed
-bar
+foo
Accept new value? [y,n,?] y
.
Finished in 1.0 seconds
1 test, 0 failures
Your test will be automatically updated:
assert_value "foo\n" == """
foo
"""
Benefits:
- makes writing tests easier by automatically generating expected values
- makes maintaining tests and refactoring code much easier
- improves test readability
You will find usage examples and documentation in the README on GitHub.
We appreciate all feedback.
P.S. We also have a ruby version of this library. The elixir version turned out to be a substantial improvement over the ruby version. Because of macros we are able to use more natural, composable, and extensible syntax. Another huge advantage is async tests and assert_value supports them fully.
Most Liked
michalmuskala
This reminds me very much the idea of approval tests.
http://approvaltests.com/ and https://github.com/kytrinyx/approvals
tmbb
This is very, very cool. Mutating the test file interactively is a great way of going over the natural laziness of writing tests in cases that are not good for property testing. There seems to be a limitation in which it only works for strings, right? Why don’t you just convert the value into an Erlang term, store it into a file (or even write it inline)? It’s strictly better than using strings in my opinion.
Now, I might have a use for this. I’m the author of the Makeup syntax highlighting library. It needs some automated tests (badly, it’s > 1000k lines of code with almost no tests), as currently all testing is visual integration testing by examining the output (there is some low hanging fruit to be had from property testing, but that’s limited).
assert_value seems like a good tool for the job: I can just dump some source code snippets somewhere, run assert_value on them, compare the output (at least the tokenization, comparing the HTML output is harder). Ultimately, this is waht I’d like to have:
I’m comparing the visual output for my program, and it happens to be in HTML. I’d like to select some code snippets, have them converted into HTML and shown in some form of HTML canvas somewhere so that I could mark them as correct. The most immediate way I can think of implement this is to have the tests start an HTTP server, start a web browser and then ask the user if the output is correct or not.
For example (ridiculous mockup I’ve done in 5mins):
An then repeat for all test cases.
joshtaylor
This is awesome - had a similar idea recently about updating tests like this, so glad somebody else made it! Awesome work!
brightball
As somebody who generally hates unit tests because of exactly the problem this solves…thank you
smetana
New version 0.8.2 has been released
New Features
Previous version supported only argument types which implemented String.Chars protocol on the left and string heredocs on the right. 0.8.2 supports all argument types (e.g. Integer, List, Map) except not serializable (e.g. Function, PID, Reference) So now you can write
assert_value 2 + 2 == 4
assert_value %{a: true} == %{a: true}
Enhancements
- Better parser now supports any kind of formatting (expressions, parentheses, multi-line, etc.)
- Better formatter (smarter formatting one-line and multi-line strings)
- Better error reporting
- Ensure compatibility with Elixir 1.6
Upgrade Instructions
- Run
mix test. You may get diffs because assert_value no longer converts everything to a string - Run
ASSERT_VALUE_ACCEPT_DIFFS=reformat mix testto take advantage of improved formatter - Add this to .formatter.exs to make Elixir formatter not to add parens to assert_value
[ import_deps: [:assert_value] ]








