Ocaml, vscode, unit tests to fake repl

One of my favorite techniques in Rust / IntelliJ is writing a code fragment of the form:

#[test]
fn blahblahblah() {
...
}

then IntelliJ will give me a green triangle in the lhs gutter, which if I click, it will run the function as a Unit test. (If no assert fails, it passes; if any assert fails, it’s an error).

I’m wondering if there is something similar in VSCode / OCaml.

The main requirements I want are:

  1. the code for the unit test is embedded in the same *.ml file as the source code

  2. VSCode gives me a green arrow or sorts on the side, which when I click, it runs this one particular test

The goal here is to have a workflow of: write a func, write a test, … and have everything all in one file and easy to run a particular test

You can write put tests right in your source code using inline tests. That link is to the dune docs on how to set it up.

It’s not too easy to run a single test with inline_tests and dune (see here and here), but inline_test style unit tests generally run fast enough.

Not sure about a little green jetbrains style triangle in vscode, but in emacs I have a shortcut that runs the tests in the current directory, which is close enough for me. You could probably set something up like that in vscode as well.

1 Like