# Limiting tests executed during package build when generating opam files with dune

**URL:** <https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184>\
**Category:** Learning\
**Tags:** dune\
**Created:** [August 3, 2020, 12:26pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184 "2020-08-03T12:26:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikoni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mikoni/32/2280_2.png) [@mikoni](https://discuss.ocaml.org/u/mikoni)\
**Post date:** [August 3, 2020, 12:26pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/1 "2020-08-03T12:26:31Z")

</div>

Is there a way to limit which tests get executed by default by `dune`? Especially in case when generating `opam` files.

Basically my use case is that, in `letters`, I have some tests that I would like to execute always, while some tests I would like to skip and expect to be executed manually. I don’t want CI to run some of the tests, because they need non-released version of a dependency and depend on an availability of an external service (I’m using ethereal.email to test that I can successfully send some emails).

I would prefer `dune` to generate `opam` files, but I don’t see a way to limit which tests gets executed when doing the release. Instead of having:

```auto
build: [
  ["dune" "subst"] {pinned}
  [
    "dune"
    "build"
    "-p"
    name
    "-j"
    jobs
    "@install"
    "@runtest" {with-test}
    "@doc" {with-doc}
  ]
]

```

I would like to have something like:

```auto
build: [
  ["dune" "subst"] {pinned}
  [
    "dune"
    "build"
    "-p"
    name
    "-j"
    jobs
    "@install"
    "@test/runtest" {with-test}
    "@doc" {with-doc}
  ]
]

```

The difference is with the to have `@test/runtest` instead of `@runtest`. I haven’t been able to find anything from the documentation that would allow me to change this part of the generated `opam` file.

Thanks.

---

<div class="post-metadata">

**Author:** ![CraigFe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craigfe/32/1989_2.png) [@CraigFe](https://discuss.ocaml.org/u/CraigFe)\
**Post date:** [August 3, 2020, 1:36pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/2 "2020-08-03T13:36:49Z")

</div>

You can use Dune’s [`.opam.template`](https://dune.readthedocs.io/en/stable/opam.html#opam-template) feature to achieve this:

- create a `letters.opam.template` file that contains _just_ the `build` field that you want to specify;
- run `dune build @install`.

Any fields in the template file will be inserted verbatim into the corresponding opam file, overriding Dune’s auto-generation for those fields.

---

<div class="post-metadata">

**Author:** ![avsm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/avsm/32/6_2.png) [@avsm](https://discuss.ocaml.org/u/avsm)\
**Post date:** [August 3, 2020, 2:32pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/3 "2020-08-03T14:32:53Z")

</div>

I’d recommend that your default `runtest` stanza remains as the set of core tests that you always want to run. You can define a custom alias (e.g. `longtests`) for the ones that require a specific environment.

The reason for this is that if you compositionally embed `letters` inside a larger monorepo, a toplevel `dune runtest` will still do a sensible thing. This is a convention and not a rule, but a useful one, as running tests from within a monorepo is an extremely quick way to iterate on large changes that span multiple libraries.

---

<div class="post-metadata">

**Author:** ![mikoni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mikoni/32/2280_2.png) [@mikoni](https://discuss.ocaml.org/u/mikoni)\
**Post date:** [August 3, 2020, 8:28pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/4 "2020-08-03T20:28:08Z")

</div>

@avsm, what you described sounds like what I would actually want to achieve, but it is not clear to me how to get there.

If I have folders `test` and `service-test` with `dune` files declaring them as tests, how could I prevent execution of `service-test` when running `dune runtest`? Should I use a custom stanza in the `dune` file of `service-test`. Does that mean that instead of having `(test ...)` I should use my custom stanza? Do you have any pointers to good examples?

Thanks.

---

<div class="post-metadata">

**Author:** ![CraigFe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craigfe/32/1989_2.png) [@CraigFe](https://discuss.ocaml.org/u/CraigFe)\
**Post date:** [August 3, 2020, 8:44pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/5 "2020-08-03T20:44:30Z")

</div>

For tests that _aren’t_ expect tests, the `(test)` stanza is sugar for:

- setting up an executable
- adding a rule to run the executable with alias `runtest`.

To create a test that runs with `dune build @runtest-all` (and not `dune build @runtest`), you can set up these stanzas:

```nohighlight
(executable
 (name main)
 (libraries alcotest))

(rule
 (alias runtest-all)
 (action (run ./main.exe)))

```

Your tests in `test/` would then want two `rule` stanzas, one for each alias that you create for them.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [August 3, 2020, 9:36pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/6 "2020-08-03T21:36:12Z")

</div>

Anil, I’ve noticed that some packages have tests as part of their build that take … inordinately long. E.g. `elpi`. Is there some sort of rule for how long tests can run? Maybe it might be good to provide a worked-example of how to separate tests into “for the install” and “for developers when building/modifying” ? Just a thought.

---

<div class="post-metadata">

**Author:** ![mikoni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mikoni/32/2280_2.png) [@mikoni](https://discuss.ocaml.org/u/mikoni)\
**Post date:** [August 26, 2020, 3:14pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/7 "2020-08-26T15:14:01Z")

</div>

It took me a while to test and make sure everything works as expected also through the release process.

Now I can keep my service tests in the codebase and simply expect them to be executed manually.

Thanks for the advise.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [August 26, 2020, 4:59pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/8 "2020-08-26T16:59:44Z")

</div>

Kakadu@ pointed out that for both deps and build rules, you can specify “with-test” and it’ll only be execued when you do “opam install -t package-name” There are many examples in opam-repository of this idiom.

---

<div class="post-metadata">

**Author:** ![rgrinberg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rgrinberg/32/40_2.png) [@rgrinberg](https://discuss.ocaml.org/u/rgrinberg)\
**Post date:** [August 26, 2020, 9:06pm UTC](https://discuss.ocaml.org/t/limiting-tests-executed-during-package-build-when-generating-opam-files-with-dune/6184/9 "2020-08-26T21:06:18Z")

</div>

There seems to be something missing in dune here. If you use a custom alias like `longtests` you will have plenty of issues with dune’s other test definition stanzas that require `runtest`. For example, there’s no way to run inline tests with `longtests`.

Another workaround is to add `(enabled_if (= %{profile} dev))` for some of these tests. This will make sure that they don’t run in release mode.
