# Automated tests for Stan functions

**URL:** <https://discourse.mc-stan.org/t/automated-tests-for-stan-functions/27466>\
**Category:** General\
**Created:** [May 13, 2022, 1:52pm UTC](https://discourse.mc-stan.org/t/automated-tests-for-stan-functions/27466 "2022-05-13T13:52:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsusmann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/hsusmann/32/11775_2.png) [@hsusmann](https://discourse.mc-stan.org/u/hsusmann)\
**Post date:** [May 13, 2022, 1:52pm UTC](https://discourse.mc-stan.org/t/automated-tests-for-stan-functions/27466/1 "2022-05-13T13:52:55Z")

</div>

Hello,  
Is there a recommended way to automate testing for hand-written Stan functions?

For example, say I define a new Stan function:

```nohighlight
functions {
  real my_square(real x) {
    return square(x);
  }

```

I would like to verify that the output of this function is correct for a set of inputs via some sort of automated procedure.

So far, the two options I can think of are:

- Use the `rstan` interface and `expose_stan_functions` to be able to directly call the function in R, at which point the output can be compared to some reference output.
- Write a dummy model in Stan that serves only to execute the function on a set of inputs, and then outputs the results via `print`. You could perhaps implement this in a `generated data` block, and have `parameters` and `model` be empty. The output could then be read externally and checked against some reference.

Are there any other options I’ve missed?

Thanks,

- Herb

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [May 13, 2022, 1:59pm UTC](https://discourse.mc-stan.org/t/automated-tests-for-stan-functions/27466/2 "2022-05-13T13:59:16Z")

</div>

Two additional options using cmdstan:

A `cmdstanr` solution due to @rok_cesnovar

> <https://gist.github.com/rok-cesnovar/c91d631b66c1124ae7f221155f87516f>

And a CLI solution to return log probabilties from cmdstan due to @syclik and @dmuck

> **[Smuggling log probability and gradients out of Stan programs — ReddingStan](https://blog.mc-stan.org/2022/03/24/smuggling-log-probability-and-gradients-out-of-stan-programs-reddingstan/)**
>
> ReddingStan smuggles log probabilities and gradients out of a Stan program. It’s meant to facilitate algorithmic prototyping.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [May 13, 2022, 2:03pm UTC](https://discourse.mc-stan.org/t/automated-tests-for-stan-functions/27466/3 "2022-05-13T14:03:28Z")

</div>

> [@jsocolar](#):
>
> A `cmdstanr` solution due to @rok_cesnovar

Thanks for pointing that out.

I have however stopped updating the gist, the latest version of that function is available [here](https://github.com/rok-cesnovar/misc/blob/master/expose-cmdstanr-functions/expose_cmdstanr_functions.R), together with a [vignette](https://rok-cesnovar.github.io/misc/exposing_cmdstanr_udf.html).
