It's a shame that including unit tests makes the snippet non-trivial to copy quite fast.
Rust has a nice solution for this though: tests can also be embedded in documentation comments:
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}
From: https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-...More languages could adopt that idea, and a good StackOverflow answer would include those tests in the snippet. StackOverflow might even automatically run the tests and add a passing/failing badge!