When we ask users to add their code as a minimal reproducible example via Stack Snippets and tell us what they expect the code to do, the snippet is quite helpful but we have no way to verify if an answer actually solves the problem. That's why I think, there should be something like tests or simplier an expected result.
Options
Tests
Some code playgrounds like codesandbox even run complete testing suites like Jest. Might be more complicated to implement but is more flexible.
Expected result
I contributed to foundeo/cfdocs
a while ago and they have tests that check the last line against what is supplied via the examples.result
key. Pretty simple, could work in simple use cases. Data structures like array and objects have to serialized but that's okay.
Feedback collected from comments
Pros | Cons |
---|---|
Answers can be sorted, compared and weighted better | Stack snippets only apply to a minority of languages |
Answer posters can check their code before posting | Ineffective/restricted output |
Multiple possible solutions | |
Additional time needed to write the tests |
Prospects
Maybe there could be some simple benchmarks, too. That'd add another criterium to sort answers. Some questions already have answers doing that manually so they could really benefit from it.