Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug unit test while developping a package in Julia

Say I develop a package with a limited set of dependencies (for example, LinearAlgebra). In the Unit testing part, I might need additional dependencies (for instance, CSV to load a file). I can configure that in the Project.toml all good.

Now from there and in VS Code, how can I debug the Unit tests? I tried running the "runtests.jl" in the debugger; however, it unsurprisingly complains that the CSV package is unavailable.

I could add the CSV package (as a temporary solution), but I would prefer that the debugger run with the configuration for the unit testing; how can I achieve that?

As requested, here is how it can be reproduced (it is not quite minimal, but instead I used a commonly used package as it give confidence the package is not the problem). We will use DataFrames and try to execute the debugger for its unit tests.

  • Make a local version of DataFrames for the purpose of developing a feature in it. I execute dev DataFrames in a new REPL.
  • Select the correct environment (in .julia/dev/DataFrames) through the VS-code user interface.
  • Execute the "proper" unit testing by executing test DataFrames at the pkg prompt. Everything should go smoothly.
  • Try to execute the tests directly (open the runtests.jl and use the "Run" button in vs-code). I see some errors of the type:
LoadError: ArgumentError: Package CategoricalArrays not found in current path:
- Run `import Pkg; Pkg.add("CategoricalArrays")` to install the CategoricalArrays package.

which is consistent with CategoricalArrays being present in the [extras] section of the Project.toml but not present in the [deps].

  • Finally, instead of the "Run" command, execute the "Run and Debug". I encounter similar errors here is the first one:
Test Summary: | Pass  Total
merge         |   19     19
        PASSED: index.jl
        FAILED: dataframe.jl
LoadError: ArgumentError: Package DataStructures not found in current path:
- Run `import Pkg; Pkg.add("DataStructures")` to install the DataStructures package.

So I can't debug the code after the part requiring the extras packages.

  • After all that I delete this package with the command free DataFrames at the pkg prompt.

I see the same behavior in my package.

like image 456
call me Steve Avatar asked Nov 14 '25 16:11

call me Steve


2 Answers

I'm not certain I understand your question, but I think you might be looking for the TestEnv package. It allows you to activate a temporary environment containing the [extras] dependencies. The discourse announcement contains a good description of the use cases.

like image 172
tholy Avatar answered Nov 17 '25 06:11

tholy


Your runtest.jl file should contain all necessary imports to run tests.

Hence you are expected to have in your runtests.jl file lines such as:

using YourPackageName
using CSV

# the lines with tests now go here.

This is a standard in Julia package layout. For an example have a look at any mature Julia such as DataFrames.jl (https://github.com/JuliaData/DataFrames.jl/blob/main/test/runtests.jl).

like image 24
Przemyslaw Szufel Avatar answered Nov 17 '25 07:11

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!