Skip to content

Tracking issue for eRFC 2318, Custom test frameworks #50297

Open

Description

@Centril

View all comments

This is tracking the #[bench] and #[test_case] attributes and the custom_test_frameworks lang item as well as the libtest crate. There is currently no plan/RFC for stabilizing them.

Old issue description.

Details

This is a tracking issue for the eRFC "Custom test frameworks" (rust-lang/rfcs#2318).

Documentation:

Steps:

Unresolved questions:

Notes:

Implementation history:

Pinned by epage

Activity

  1. added
    B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.
    T-dev-toolsRelevant to the dev-tools subteam, which will review and decide on the PR/issue.
    T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.
    A-libtestArea: `#[test]` / the `test` library
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Apr 28, 2018
  2. CAD97 commented on May 14, 2018

    @CAD97
    Contributor

    On stdout/err capture (after doing some research around the proof-of-concept #50457):

    The current state is sub-optimal, to say the least. Both stdout and stderr exist as thread-local handles that only the stdlib has access to, which print family of macros use, as does panic.

    Relevant libstd snippets

    thread_local! {
    static LOCAL_STDOUT: RefCell<Option<Box<Write + Send>>> = {
    RefCell::new(None)
    }
    }

    thread_local! {
    pub static LOCAL_STDERR: RefCell<Option<Box<Write + Send>>> = {
    RefCell::new(None)
    }
    }

    rust/src/libstd/io/stdio.rs

    Lines 614 to 711 in 935a2f1

    /// Resets the thread-local stderr handle to the specified writer
    ///
    /// This will replace the current thread's stderr handle, returning the old
    /// handle. All future calls to `panic!` and friends will emit their output to
    /// this specified handle.
    ///
    /// Note that this does not need to be called for all new threads; the default
    /// output handle is to the process's stderr stream.
    #[unstable(feature = "set_stdio",
    reason = "this function may disappear completely or be replaced \
    with a more general mechanism",
    issue = "0")]
    #[doc(hidden)]
    pub fn set_panic(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
    use panicking::LOCAL_STDERR;
    use mem;
    LOCAL_STDERR.with(move |slot| {
    mem::replace(&mut *slot.borrow_mut(), sink)
    }).and_then(|mut s| {
    let _ = s.flush();
    Some(s)
    })
    }
    /// Resets the thread-local stdout handle to the specified writer
    ///
    /// This will replace the current thread's stdout handle, returning the old
    /// handle. All future calls to `print!` and friends will emit their output to
    /// this specified handle.
    ///
    /// Note that this does not need to be called for all new threads; the default
    /// output handle is to the process's stdout stream.
    #[unstable(feature = "set_stdio",
    reason = "this function may disappear completely or be replaced \
    with a more general mechanism",
    issue = "0")]
    #[doc(hidden)]
    pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
    use mem;
    LOCAL_STDOUT.with(move |slot| {
    mem::replace(&mut *slot.borrow_mut(), sink)
    }).and_then(|mut s| {
    let _ = s.flush();
    Some(s)
    })
    }
    /// Write `args` to output stream `local_s` if possible, `global_s`
    /// otherwise. `label` identifies the stream in a panic message.
    ///
    /// This function is used to print error messages, so it takes extra
    /// care to avoid causing a panic when `local_stream` is unusable.
    /// For instance, if the TLS key for the local stream is
    /// already destroyed, or if the local stream is locked by another
    /// thread, it will just fall back to the global stream.
    ///
    /// However, if the actual I/O causes an error, this function does panic.
    fn print_to<T>(
    args: fmt::Arguments,
    local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
    global_s: fn() -> T,
    label: &str,
    )
    where
    T: Write,
    {
    let result = local_s.try_with(|s| {
    if let Ok(mut borrowed) = s.try_borrow_mut() {
    if let Some(w) = borrowed.as_mut() {
    return w.write_fmt(args);
    }
    }
    global_s().write_fmt(args)
    }).unwrap_or_else(|_| {
    global_s().write_fmt(args)
    });
    if let Err(e) = result {
    panic!("failed printing to {}: {}", label, e);
    }
    }
    #[unstable(feature = "print_internals",
    reason = "implementation detail which may disappear or be replaced at any time",
    issue = "0")]
    #[doc(hidden)]
    pub fn _print(args: fmt::Arguments) {
    print_to(args, &LOCAL_STDOUT, stdout, "stdout");
    }
    #[unstable(feature = "print_internals",
    reason = "implementation detail which may disappear or be replaced at any time",
    issue = "0")]
    #[doc(hidden)]
    pub fn _eprint(args: fmt::Arguments) {
    use panicking::LOCAL_STDERR;
    print_to(args, &LOCAL_STDERR, stderr, "stderr");
    }


    The existing stdout/err capture for the test harness works by setting the thread-local, which, if set, the print family of macros will use, otherwise falling back to io::stdout(). This setup is problematic because it's fairly trivial to escape this (#12309) and 99.9% of logging frameworks will escape accidentally (#40298).

    The reason for this is that any solution that's generic over io::Write is going to use io::stdout() as the sink to hit stdout, and that doesn't go through the tread-local plumbing, so hits the true stdout directly.

    The way I see it, there are three ways to resolve this quirk:

    • Create a new stdlib io::Write sink that uses the thread-local plumbing that the print macros do today, and implement the print macros as writing to that sink. That's the approach I proved was possible with Reusable handle to thread-local replaceable stdout/err #50457.
    • Create a new io::Write sink that uses (e)print! behind the scenes. This is possible entirely in an external crate:
    (E)PrintWriter - feel free to use these, I am the author and release them under MIT/Unlicense
    /// IO sink that uses the `print` macro, for test stdout capture.
    pub struct PrintWriter;
    impl std::io::Write for PrintWriter {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            print!("{}", String::from_utf8_lossy(buf));
            Ok(buf.len())
        }
        fn flush(&mut self) -> std::io::Result<()> { std::io::stdout().flush() }
    }
    
    /// IO sink that uses the `eprint` macro, for test stdout capture.
    pub struct EPrintWriter;
    impl std::io::Write for EPrintWriter {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            eprint!("{}", String::from_utf8_lossy(buf));
            Ok(buf.len())
        }
        fn flush(&mut self) -> std::io::Result<()> { std::io::stdout().flush() }
    }

    Unfortunately, this suffers from a large number of drawbacks. Because we only have access to the print macro, we have to interpret whatever sequence of bytes the sink gets as a UTF-8 string, even though that's potentially not true, etc. etc. not zero-cost when it's not, and so on. It's a decent solution for hooking up logging in tests, but is very much not a general solution that could be used outside tests.

    That takes us to the third option, which is almost certainly the best, but also the most complicated:

    • Actually change stdout/err

    It sounds simple, but it's a lot simpler said than done with the current test harness setup. io::Stderr boils down to a handle to a write stream available to the process, and is a process-local resource -- there's only one stdout per process. The test harness currently runs tests parallel on different threads within the same process, thus the current thread-local replacement.

    The other issue is that the test harness doesn't just want to capture stdout of the tests, it wants to capture stdout of the tests separately for each test, so it can print just the stdout of the failing test(s) and not have the other tests' output interspersed with it.

    The only solution I see at this time that maintains current behavior of separate captures per test run is to run them all in their own process, and capture stdout that way. That way each process can pipe its stdout/err to the test runner, which can then synchronize them and decide which ones to output to the true stdout/err if desired.

    A sketch of the plumbing required to make an out-of-tree libtest that works with this behavior:

    • #[test] fn are all collected into a list
    • A fn main is generated that takes an argument of one of the paths, and runs that test
    • (The existing fn main is hidden)
    • As normal, successful execution is success, and a panic is a failure exit code
    • Our runner calls the generated binary with each of the test paths that match any filtering done as separate processes, captures their stdout/err and reports them on failure

    Note that this is unfortunately stateful: we need to fold over all #[test] fn before we can emit the proper main. I'm not knowledgeable enough around compiler internals to be much help implementing the machinery right now (though I'd love to learn my way around and help out!), but give me an API to build against and I'll definitely help with any test frameworks built against the plumbing the eRFC has us set to experiment with.

  3. CAD97 commented on May 14, 2018

    @CAD97
    Contributor

    TL;DR:

    To have sane capture behavior for stdout and stderr for print-family macros, panic, and io::{stdout, stderr}, running each test in its own (child) process is probably required. I'd love to help out wherever I can, but lack confidence.

  4. retep998 commented on May 15, 2018

    @retep998
    Contributor

    The only solution I see at this time that maintains current behavior of separate captures per test run is to run them all in their own process, and capture stdout that way.

    This would be significantly slower on certain platforms where process creation is slow, notably Windows. Especially when the tests are all very quick this will cause severe regressions in total time.

  5. CAD97 commented on May 15, 2018

    @CAD97
    Contributor

    Another possibility for process separation of tests is to spawn a process for each unit of parallelism, but each process is reused for multiple tests.

    Of course, we could also just promote the thread-local replaceable stdout/err to io::Stdout/Err, but that would be suboptimal itself.

    The issue is that stdout is, as far as I can tell, a process-local resource. To capture io::Stdout we either need our own stdout replacement machinery on top or to separate processes.

  6. SoniEx2 commented on Jun 13, 2018

    @SoniEx2
    Contributor

    if I want to argue about this do I argue about it here?

    so like I made hexchat-plugin crate. the problem is you can't test hexchat side without hexchat. so I'd need some sort of #[hexchat_test].

    but I still want the users to be able to use plain old vanilla rust #[test]s. I shouldn't have to reimplement/replace the whole testing system just to be able to produce some hexchat-based unit tests (in addition to normal tests).

    (disclaimer: I don't fully understand the RFC, but it seems to require either replacing everything, or not having tests)

  7. jonhoo commented on Jun 13, 2018

    @jonhoo
    Contributor

    @SoniEx2 this RFC is about how we'd like to change the underlying mechanism that is used to arrange testing in Rust so that authors can write new test frameworks (e.g., quickcheck) that have access to the same mechanisms as existing built-in tests ( able to access private functions, can find all tagged functions, etc.). The current proposal is relatively wholesale (you change the test framework for a whole crate), but this is something that may be improved upon. We decided against more sophisticated designs in the initial implementation phase (this is just an "experimental" RFC) so that we don't bite off more than we can chew, but I think down the line we'd want more fine-grained test framework selection too.

    As for your specific example, you don't actually need custom frameworks for that feature :) It seems like you want to only run a test if hexchat isn't running? To do that, you could either mark the test with #[allow_fail], or you could have the test just return if it fails to connect. If you wanted a more fine-grained approach, you could write a procedural macro that re-writes any function annotated with #[hexchat_test] into a #[test] that returns if hexchat isn't running.

  8. SoniEx2 commented on Jun 13, 2018

    @SoniEx2
    Contributor

    no you don't understand, they have to run inside hexchat. loaded with dlopen (or equivalent). after plugin registration.

  9. djrenren commented on Aug 7, 2018

    @djrenren
    Contributor

    Hey all, I've come up with a simplified proposal and I have an implementation now (though it needs a little cleaning up before a PR).

    https://blog.jrenner.net/rust/testing/2018/08/06/custom-test-framework-prop.html

    Lemme know what you think!

  10. CAD97 commented on Aug 21, 2018

    @CAD97
    Contributor

    I've thought some more about stdin/stderr capture. Here's a draft of a system that should be able to "perfectly" capture stdin/stdout, though not without O(1) (once per test suite) overhead:

    • The main process is started by cargo test. It does no actual testing; its job is to coordinate child processes to do the actual tests.
    • At startup of the main process, it spawns a child process for each unit of parallelism in the test runner. This is a one-time cost, as these child processes are reused for multiple tests.
    • With each child process spawned, a communication channel is created between the processes distinct of stdin/out/err. This is used to instruct the child process which test to run next, and for the child process to report back the success/failure of the test.
    • We avoid talking to the child process on stdin because the test could listen to stdin, and we want to avoid accidentally polluting that. (Also, we need a back-channel that's not stdout to communicate test results anyway.)
    • The stdout/stderr of the child processes are piped into communication channels into the main process, which handles reporting those if the test failed.
    • Tests are run off the main thread in child processes, so that the child process doesn't die to a panic and that it can (or the main thread can instruct it to) terminate runaway tests early.
    • After the main process has handed out all tests to the child processes, it shuts them down as it receives the final reports, and then outputs the final report to the user before terminating.
  11. jonhoo commented on Aug 21, 2018

    @jonhoo
    Contributor

    @djrenren finally had a chance to read your proposal. I'm excited by the progress on this, though your write-up did raise some questions for me:

    • Early on, you say that "Annotated items must be a nameable const, static, or fn.". Further down, when you discuss the use-case of a framework that supports test suites, you show a #[test_suite] annotation on a mod with #[suite_member] on the nested tests. It'd be good to show how that expansion works. Does a proc macro turn #[test_suite] into a const with an element for each #[suite_member]? How does it find all of them? Are the #[suite_member]s all turned into #[test_case]? How do you avoid double-executing those test cases (once as a "child" and once due to the #[test-case] annotation)?
    • For #![test_runner] you say "The type of the function must be Fn(&[&mut Foo]) -> impl Termination for some Foo which is the test type", yet further down you write a Fn(&[&dyn Foo]). I assume the latter is the correct version?
    • Is TestDescAndFn sufficient for our purposes here? I don't think that can (currently) wrap a struct or const in a meaningful way?
    • I find the "test runner" and "test format" examples a little backwards. To me, "formatting" is about output, and "running" is about the structure of the tests themselves (and how they're run). We can bikeshed names later though.
    • The "editor wants to integrate with testing" example makes me sad. It seems really sad for the crate-under-test to have to be modified to work with my editor. One of the earlier RFCs had a section on separating the test harness from the test executor, with the former controlling output and the latter how tests are written and executed. And crucially, the former should be possible to swap without changing the source code of the c-u-t. That doesn't solve the case of an editor wanting to control test execution as well, but I think ideally we'd be able to fold that into the harness somehow too.
  12. 121 remaining items

  13. added
    S-tracking-needs-summaryStatus: It's hard to tell what's been done and what hasn't! Someone should do some investigation.
    on May 4, 2025
  14. added a commit that references this issue on May 7, 2025
  15. added a commit that references this issue on May 7, 2025
  16. added a commit that references this issue on May 16, 2025
  17. mrchantey commented on Dec 16, 2025

    @mrchantey

    If I understand correctly the issue of doctests being excluded from custom test frameworks would be resolved by #141083 because in this case they get converted to a regular #[test] in the host crate.
    @fmease can you confirm this?

  18. added a commit that references this issue on Mar 7, 2026
  19. added a commit that references this issue on Mar 7, 2026
  20. epage commented on Nov 2, 2026

    @epage
    Contributor

    See rust-lang/testing-devex-team#2

    I went to emphasize this real quick.

    From what I understand of eRFC 2318 is that it exposes some of the libtest and compiler machinery to end users. Most of these mechanisms are wanted outside of testing and this is fairly restrictive in terms of what can be done for testing. The current thought is to explore custom test harnesses through Cargo's harness = false (like libtest-mimic), see what more general features are needed, and iterate from there.

    For example, maybe test discovery can instead be done through externally implementable items (#125418)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryB-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCF-custom_test_frameworks`#![feature(custom_test_frameworks)]`S-tracking-design-concernsStatus: There are blocking design concerns.S-tracking-needs-summaryStatus: It's hard to tell what's been done and what hasn't! Someone should do some investigation.T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.T-dev-toolsRelevant to the dev-tools subteam, which will review and decide on the PR/issue.T-langRelevant to the language teamT-testing-devexRelevant to the testing devex team (testing DX), which will review and decide on the PR/issue.WG-embeddedWorking group: Embedded systems

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions