StatAgentWeekly email

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

Task
Programming
Roles
Programmer
Assistants
Microsoft 365 Copilot, ChatGPT, Claude
Last reviewed

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

  1. Paste the function and its intended behaviour.
  2. Ask for testthat tests with edge cases.
  3. Run the tests.
  4. Fix invalid tests.
  5. 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

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

Related

How we check. Back to the directory.