# Running unit tests for stan\_math: how to avoid recompilation

**URL:** <https://discourse.mc-stan.org/t/running-unit-tests-for-stan-math-how-to-avoid-recompilation/12150>\
**Category:** Developers\
**Tags:** math\
**Created:** [December 2, 2019, 8:05pm UTC](https://discourse.mc-stan.org/t/running-unit-tests-for-stan-math-how-to-avoid-recompilation/12150 "2019-12-02T20:05:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Van\_Horn](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kevin_van_horn/32/4513_2.png) [@Kevin\_Van\_Horn](https://discourse.mc-stan.org/u/Kevin_Van_Horn)\
**Post date:** [December 2, 2019, 8:05pm UTC](https://discourse.mc-stan.org/t/running-unit-tests-for-stan-math-how-to-avoid-recompilation/12150/1 "2019-12-02T20:05:15Z")

</div>

I’m looking at extending gaussian\_dlm\_obs to allow time-varying system matrices. I ran the stan\_math unit tests initially using runTests.py. This takes very long, with most of the time spent compiling the tests. Then, after making some changes to gaussian\_dlm\_obs\_lpdf.hpp, I ran runTests.py again. I was expecting that only a few things would be recompiled, those directly affected by the change to the header file, but instead it seems to be recompiling _everything_. Is this the expected behavior? Is there any way to get make-like behavior where only the things that need to be recompiled get recompiled?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [December 2, 2019, 8:22pm UTC](https://discourse.mc-stan.org/t/running-unit-tests-for-stan-math-how-to-avoid-recompilation/12150/2 "2019-12-02T20:22:51Z")

</div>

> [@Kevin\_Van\_Horn](#):
>
> Is this the expected behavior? Is there any way to get make-like behavior where only the things that need to be recompiled get recompiled?

For some definition of expected. Many of the tests first `#include <stan/math.hpp>`, which in turn includes almost every file under `rev` or `prim`, and so `make` thinks everything needs to be recompiled. It is this way because the order of the included headers matters in Stan Math and just putting `#include <stan/math.hpp>` on the top line was considered “easier” for developers to get things to compile correctly, even though it then becomes impractical for developers to run all the unit tests on their own machine.

For something like gaussian\_dlm\_obs\_lpdf.hpp, which isn’t used by anything else, I would just call

```no-highlight
./runTests.py test/unit/math/prim/mat/prob/gaussian_dlm_obs_log_test.cpp

```

and once that is working, push the branch so that all the tests get run on a server.

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [December 3, 2019, 1:29pm UTC](https://discourse.mc-stan.org/t/running-unit-tests-for-stan-math-how-to-avoid-recompilation/12150/3 "2019-12-03T13:29:20Z")

</div>

> [@Kevin\_Van\_Horn](#):
>
> I was expecting that only a few things would be recompiled, those directly affected by the change to the header file, but instead it seems to be recompiling _everything_ . Is this the expected behavior?

Yes, this is the current expected behavior. In order to ease the burden on developers, we’ve gone with a simpler include in the unit tests. As @bgoodri outlined, this pulls in almost all of the headers in one go, so there’s no way for `make` (through an invocation of the C++ compiler) to know that only a small portion of the tests have changed.

At some point, we can change how the tests are linked to make this convenient for running again. (It used to be that way a few versions ago.)

> [@bgoodri](#):
>
> For something like gaussian\_dlm\_obs\_lpdf.hpp, which isn’t used by anything else, I would just call
> 
> ```no-highlight
> ./runTests.py test/unit/math/prim/mat/prob/gaussian_dlm_obs_log_test.cpp
> 
> ```
> 
> and once that is working, push the branch so that all the tests get run on a server.

+1. That’s the fast way to get things unit tested these days.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [December 7, 2019, 2:41am UTC](https://discourse.mc-stan.org/t/running-unit-tests-for-stan-math-how-to-avoid-recompilation/12150/4 "2019-12-07T02:41:25Z")

</div>

> [@Kevin\_Van\_Horn](#):
>
> but instead it seems to be recompiling _everything_ . Is this the expected behavior?

Yes. To expand on what @bgoodri and @syclik said, we’re moving to including the top level `stan/math.hpp` in our tests rather than little bits and pieces because we were continually getting bit by things working with only a few includes, but not working with the whole library due to things like ambiguity.
