TestItems falling at the first hurdle

Hi, I’m very new to Julia and I thought I’d get started by creating a little toy project. I saw a recommendation somewhere to use the TestItems package to set up unit tests in VS code.

I’ve had a little trouble getting this working in the way described by various pieces of documentation (for example, the julia-vscode site and testitemrunner package site).

I can get the package to work by fully qualifying all my function names - but according to these sources this doesn’t look like it’s necessary, so I’m concerned my setup is somehow wrong or I’m misunderstanding something critical about how packages make their symbols available.

For instance, if I try to test the following code:

module MyPackage

using TestItems

function foo(x)
    return x*x
end

@testitem "Test for foo" begin
    x = foo("bar")

    @test x == "barbar"
end

end

I get an error:

My best guess is that some aspect of my environment is wrong and so the TestItems framework is not aware of what package I’m developing, and therefore does not bring the foo symbol into scope. But given that the snippet I shared is one of the first examples and is presented as if it should work right out of the box, I’m a little confused about where to go next. Does anyone have any ideas what might be wrong?

In Julia you have to export the symbols that you want to put in the scope by using MyModule. Therefore you have to either add export foo to the Module or qualify the function name by the module. See Modules · The Julia Language for more details.

1 Like

Thanks for your reply Fliks, but I don’t think that was my issue. The documentation for TestItems implies that using MyModule is implicitly invoked by the @testitems macro. In any case, I think my issue is now solved. It seems there were actually two problems - one with my setup which has resolved itself, and one to do with me not appreciating import rules for macros.

For one, the sample code that is failing above works for me now, with no changes. I’m not sure why it was failing before - I had tried restarted VS code and things like that, but closing everything and coming back after a couple of hours seems to have magically solved it. Unfortunately I cannot recreate the issue now.

Separately, when I first started using the package, I had written a simple macro and was trying to test that. I think @testitems will automatically bring exported symbols from the active package into scope, and something I hadn’t appreciated was that, apparently, macros must be explicitly imported to be used without qualification. This makes sense but because using MyPackage is hidden within @testitems, it caused me some confusion, because (after the initial problem was fixed), functions were testable but macros weren’t.

So I think the solution is that I just need to either fully qualify macro calls to test them from inside @testitems blocks, or explicitly import them within that block.

Hopefully someone will correct me if I’m wrong about any of that, but otherwise I think the problem is solved! Thanks for your input nonetheless Fliks.

You need to export foo from your module. Or call it with MyModule.foo().

1 Like