# \`\]test Package\` errors, but \`include(test\_script)\` succeeds

**URL:** <https://discourse.julialang.org/t/test-package-errors-but-include-test-script-succeeds/100326>\
**Category:** General Usage\
**Created:** [June 14, 2023, 12:15pm UTC](https://discourse.julialang.org/t/test-package-errors-but-include-test-script-succeeds/100326 "2023-06-14T12:15:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [June 14, 2023, 12:15pm UTC](https://discourse.julialang.org/t/test-package-errors-but-include-test-script-succeeds/100326/1 "2023-06-14T12:15:14Z")

</div>

When testing a package (FlashWeave.jl) via `]test FlashWeave`, I’m getting errors related to internal matrix computations (only reliant on SparseArrays.jl), but if I instead manually `include(<test_script>)` the same test script and environment (default v1.9 env), it runs without errors, even though I’m using the same julia executable and package (in `.julia/dev`).

Curiously, if I make changes to FlashWeave and `]precompile`, a subsequent `include(<test_script>)` runs as expected without precompilation, but `]test FlashWeave` triggers a new round of precompilation (before running and then producing the error above).

Any idea what could be causing this? I have a hunch that this is related to the testing environment, even though package versions should be identical to my default v1.9 environment if I understand correctly.

Julia version:

```julia
julia> versioninfo()
Julia Version 1.9.1
Commit 147bdf428cd (2023-06-07 08:27 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin22.4.0)
  CPU: 16 × Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, skylake)
  Threads: 1 on 16 virtual cores

```

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [June 14, 2023, 1:07pm UTC](https://discourse.julialang.org/t/test-package-errors-but-include-test-script-succeeds/100326/2 "2023-06-14T13:07:06Z")

</div>

I think it would be useful if you included outputs and error messages in the description of the problem. Without this, it’s hard to tell what the issue is.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [June 14, 2023, 1:26pm UTC](https://discourse.julialang.org/t/test-package-errors-but-include-test-script-succeeds/100326/3 "2023-06-14T13:26:43Z")

</div>

As far as I understand, `] test` creates a new environment with possibly new versions.

If you do `include("runtests.jl")` it will use the packages and versions of the activated env.

To speed-up `]test` I always use

> [@Testing a different file than runtests.jl](https://discourse.julialang.org/t/testing-a-different-file-than-runtests-jl/87789/10):
>
> Just dropping my testing workflow: First, I test everything with (where [TestEnv.jl](https://github.com/JuliaTesting/TestEnv.jl) is installed globally): julia\> using Pkg; Pkg.activate("."); using TestEnv; TestEnv.activate(); include("test/runtests.jl") For any further test runs, I do: julia\> include("test/subtests.jl")

---

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [June 14, 2023, 2:48pm UTC](https://discourse.julialang.org/t/test-package-errors-but-include-test-script-succeeds/100326/4 "2023-06-14T14:48:23Z")

</div>

Thanks everyone for the responses, sorry for not providing more detail. I think I figured it out in the meantime: the code in question indeed had a bounds error, but is wrapped in `@inbounds`. It seems using the `]test` workflow somehow activated boundschecks, resulting in the error, while `include` resulted in a silent out-of-bounds access that didn’t lead to any issues (in this case). Removing `@inbounds` and/or fixing the bug results in consistent behaviour.

For reference, this was the function in question:

```julia
function contingency_table_2d_optim!(X::Int, Y::Int, data::SparseArrays.AbstractSparseMatrixCSC{<:Integer}, test_obj::MiTest{<:Integer, Nz})
    # Initialize a 3x3 zero matrix to hold the contingency table
    fill!(test_obj.ctab, 0)
    rvs = rowvals(data)
    nzvs = nonzeros(data)

    # Get the pointers to the start and end of the non-zero elements in each column
    ptr_X, ptr_Y = data.colptr[X], data.colptr[Y]
    ptr_X_end, ptr_Y_end = data.colptr[X + 1], data.colptr[Y + 1]
    row_X, row_Y = rvs[ptr_X], rvs[ptr_Y]

    # While there are non-zero elements remaining in either column
    @inbounds while ptr_X < ptr_X_end && ptr_Y < ptr_Y_end        
        if row_X == row_Y
            val_X, val_Y = nzvs[ptr_X] + 1, nzvs[ptr_Y] + 1
            ptr_X += 1
            ptr_Y += 1
            row_X, row_Y = rvs[ptr_X], rvs[ptr_Y] # bounds error occurred here
            test_obj.ctab[val_X, val_Y] += 1
        elseif row_X < row_Y
            ptr_X += 1
            row_X = rvs[ptr_X]
        else
            ptr_Y += 1
            row_Y = rvs[ptr_Y]
        end
    end

    return nothing
end

```
