Generate unit tests for an R analysis function and run them
AI drafts testthat tests for an R function from its description, including edge cases. A published Java study found many generated tests invalid and we found no R or clinical test, so you must run and review every test.
Untested ideaMedium risk
Evidence
Untested idea. No evaluation for R or clinical code found. Context only: Yang et al. (arXiv 2406.18181) studied Java (Defects4J, mostly open-source code models plus GPT-4): 34.44% to 61.78% of generated tests were syntactically invalid, and GPT-4 line coverage was 40.43% versus 78.91% for EvoSuite. Different language and tooling. Prompt not run.
Steps
- Paste the function and its intended behaviour.
- Ask for testthat tests with edge cases.
- Run the tests.
- Fix invalid tests.
- Add cases the AI missed.
Prompt
This is the prompt, typed out. No assistant reply is shown.
You
Write testthat unit tests for the R function below. Include: normal cases, missing values, ties, zero-length input, and one case checked against a hand calculation shown in a comment. Expected values must come from the stated behaviour or the hand calculation; do not copy outputs from running the function. List assumptions.
Rules: use ONLY the input below. Tag anything the input does not state as OPEN QUESTION. Never invent numbers; use placeholders such as xx. Do not claim anything is verified.
INPUT:
[paste input here]
Verify before you use it
- All tests run.
- Expected values were derived independently.
- Edge cases cover missing, ties and empty input.
- Tests fail when the function is broken on purpose.
- Test file is under version control.
How it fails
Tests encode the function's current output, so they pass even when the function is wrong.
Data rule: No confidential functions or data. Use public code and simulated inputs.
Sources
- Yang et al. An Empirical Study of Unit Test Generation with Large Language Models (arXiv:2406.18181)