# Examples and Tests

**URL:** <https://internals.rust-lang.org/t/examples-and-tests/14347>\
**Category:** cargo\
**Created:** [March 26, 2021, 3:04pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347 "2021-03-26T15:04:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![orenbenkiki](https://avatars.discourse-cdn.com/v4/letter/o/53a042/32.png) [@orenbenkiki](https://internals.rust-lang.org/u/orenbenkiki)\
**Post date:** [March 26, 2021, 3:04pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/1 "2021-03-26T15:04:19Z")

</div>

I just discovered how to add example binaries to a library crate thanks to [Add examples to your Rust&nbsp;libraries](http://xion.io/post/code/rust-examples.html)

This is great, and I love the fact the examples get rebuilt automatically as part of cargo test.

That said... given that they are rebuilt, is there an idiomatic way to _use_ them in the tests? E.g., I could run the example program with some parameters and examine its output.

To do this, (1) cargo must build the examples _before_ running any test and (2) the examples should be in a predictable location.

(2) is trivial - the binaries are in target//examples/ which is easy enough (it does require me to detect which mode the test is being executed at).

Is (1) guaranteed though?

---

<div class="post-metadata">

**Author:** ![ehuss](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@ehuss](https://internals.rust-lang.org/u/ehuss)\
**Post date:** [March 26, 2021, 3:55pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/2 "2021-03-26T15:55:55Z")

</div>

Unfortunately there isn't a good story for automated testing of example programs. The examples are not guaranteed to be built for an integration test. One option now is to use a separate script to drive whatever tests you want to execute (like by running `cargo run --example foo -- somearg` and checking the output).

---

<div class="post-metadata">

**Author:** ![orenbenkiki](https://avatars.discourse-cdn.com/v4/letter/o/53a042/32.png) [@orenbenkiki](https://internals.rust-lang.org/u/orenbenkiki)\
**Post date:** [March 26, 2021, 4:11pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/3 "2021-03-26T16:11:23Z")

</div>

A pity. It seems simple enough to just build them before the tests - is this a reasonable feature request?

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [March 26, 2021, 4:25pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/4 "2021-03-26T16:25:28Z")

</div>

Currently they are built in parallel, so doing this unconditionally would make `cargo test` take longer in many cases.

You can use a crate like [escargot](https://docs.rs/escargot/0.5.1/escargot/struct.CargoBuild.html#method.example) and/or [assert\_cmd](https://docs.rs/assert_cmd/1.0.3/assert_cmd/cargo/index.html) to make your test code explicitly build and run the binaries it depends on.

---

<div class="post-metadata">

**Author:** ![orenbenkiki](https://avatars.discourse-cdn.com/v4/letter/o/53a042/32.png) [@orenbenkiki](https://internals.rust-lang.org/u/orenbenkiki)\
**Post date:** [March 26, 2021, 4:33pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/5 "2021-03-26T16:33:16Z")

</div>

That makes sense - parallel builds/tests are important. I'll try the crates you pointed at - thanks!

---

<div class="post-metadata">

**Author:** ![orenbenkiki](https://avatars.discourse-cdn.com/v4/letter/o/53a042/32.png) [@orenbenkiki](https://internals.rust-lang.org/u/orenbenkiki)\
**Post date:** [March 26, 2021, 5:04pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/6 "2021-03-26T17:04:08Z")

</div>

Hmmm, none of them seems geared towards building example binaries. Still, I can probably do something along these lines, or just have a script that runs `cargo run example ...` before running `cargo test`.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [March 26, 2021, 5:55pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/7 "2021-03-26T17:55:51Z")

</div>

The [`example`](https://docs.rs/escargot/0.5.1/escargot/struct.CargoBuild.html#method.example) method from `escargot::CargoBuild` will build an example binary.

---

<div class="post-metadata">

**Author:** ![orenbenkiki](https://avatars.discourse-cdn.com/v4/letter/o/53a042/32.png) [@orenbenkiki](https://internals.rust-lang.org/u/orenbenkiki)\
**Post date:** [March 27, 2021, 5:09am UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/8 "2021-03-27T05:09:12Z")

</div>

How did I miss that?

To provide flags and capture the output I had to change the example there of ...exec().unwrap() to ...run().unwrap().command().args(...).output().unwrap() - took a bit of trial and error to get that incantation - but now it works perfectly.

Thanks!

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [March 27, 2021, 3:41pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/9 "2021-03-27T15:41:41Z")

</div>

This topic wouldn't be complete if not mentioning that you can add `#[test]`s to your examples and make sure to add `cargo test --examples` to test them in CI. If your main function is minimal you can arrange for everything to be tested, if wanted.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 22, 2024, 5:21pm UTC](https://internals.rust-lang.org/t/examples-and-tests/14347/10 "2024-12-22T17:21:10Z")

</div>

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
