7 releases

Uses old Rust 2015

0.1.0 Oct 15, 2018
0.0.6 Oct 14, 2018

#208 in Testing

Download history 3/week @ 2022-03-05 23/week @ 2022-03-12 29/week @ 2022-03-19 1/week @ 2022-03-26 3/week @ 2022-04-02 2/week @ 2022-04-09 3/week @ 2022-04-16 9/week @ 2022-04-23 31/week @ 2022-04-30 37/week @ 2022-05-07 121/week @ 2022-05-14 25/week @ 2022-05-21 44/week @ 2022-05-28 44/week @ 2022-06-04 2/week @ 2022-06-11 5/week @ 2022-06-18

95 downloads per month

MIT license

527 lines


crates.io Docs.rs Master Doc Rustc Version Build Status

Test Rust code blocks in your Markdown files.


This crate executes the code blocks in Markdown files by embedding them into the Rust source and builds as a crate. The advantage of this approach is that the dependency resolution are done by cargo. This means that that dependency problems associated with updating the Rust toolchain do not occur.

Getting Started

doubter embeds the target Markdown files into the Rust code as the public doc comments. Therefore, it is necessary to create a new crate for testing code blocks separately from the published crates. This crate(s) are usually registered in [workspace.members].

Using Procedural Macros

Add the dependencies for doubter to Cargo.toml. If some external crates are required in code blocks, specify them as the members of [dev-dependencies]:

doubter = "0.1.0"

rand = "0.5"

Then, modify src/lib.rs to specify the path to target Markdown files.

extern crate doubter;

generate_doc_tests! {
    include = "README.md",
    include = "docs/**/*.md",

The macro generate_doc_tests!(...) takes a comma-separated list of fields. The following field keys are currently supported:

  • include - string
    A glob pattern that points to the path to the Markdown file(s) to be tested. Required to be a relative path from cargo's manifest directory.
  • mode - string, optional
    The mode to convert Markdown files to doctest. Supported values are as follows:
    • "raw" (default) : embeds the Markdown files in Rust source as it is.
    • "extract" : extracts code blocks and emit as doctest per blocks.
  • use_external_doc - string or boolean, optional
    Specify whether to use #[doc(include = "...")] to embed Markdown files. When this filed is enabled, the value of mode is forced to "raw".

Currently, the implementation of function style procedural macro is using proc-macro-hack. The definition of procedural macro via custom Derive has some restrictions and the use-style import does not work as expected. You can switch the implementation to the Macros 1.2 by disabling the feature flag hack (this feature flag is enabled by default):

version = "0.1.0"
default-features = false

Using Custom Build Script (a.k.a build.rs)

There are some restrictions on the use of procedural macros (for example, literals passed to the field cannot be calculated by using another macros). doubter provides a low level API for generating test codes from build.rs.

At first, moves the dependency on doubter to [build-dependencies]:

doubter = "0.1.0"

The code for generating test cases in build.rs looks like as follows:

extern crate doubter;

fn main() {
    let config = doubter::Config {
        includes: vec![...],
        mode: None,
        use_external_doc: false,

    let out_path = std::env::var_os("OUT_DIR")

    let mut file = std::fs::OpenOptions::new()

    doubter::generate_doc_tests(config, &mut file).unwrap();

Finally, includes the generated source into lib.rs as follows:

include!(concat!(env!("OUT_DIR"), "/doubter-tests.rs"));


See the test crates inside of crates/.


doubter is released under the MIT license.


~53K SLoC