how to remove code duplicacy while using separate sets in safetestsets

Viewed 82

I'm new to Julia and I'm trying to write tests for Genie. I'm using SafeTestsets. It can really help us separate test in different sets but now there is a lot of duplicate code because you can't access outside set's scope

For example in below example, using Genie, Genie.Assets are repeating

@safetestset "Assets functionality" begin

  @safetestset "Assets paths" begin
    using Genie, Genie.Assets

    @test include_asset(:css, "foo")  == "/css/foo.css"
    @test include_asset(:js, "foo")   == "/js/foo.js"

    @test css_asset("foo") == css("foo") == "/css/foo.css"
    @test js_asset("foo") == js("foo") == "/js/foo.js"
  end;

  @safetestset "Expose settings" begin
    using Genie, Genie.Assets

    @test js_settings() == "window.Genie = {};\nGenie.Settings = {\"webchannels_autosubscribe\":true,\"server_host\":\"127.0.0.1\",\"webchannels_subscribe_channel\":\"subscribe\",\"server_port\":8000,\"webchannels_default_route\":\"__\",\"webchannels_unsubscribe_channel\":\"unsubscribe\",\"websockets_port\":8001}\n"
  end

  @safetestset "Embedded assets" begin
    using Genie, Genie.Assets

    @test channels()[1:18] == "window.Genie = {};"
    @test channels_script()[1:27] == "<script>\nwindow.Genie = {};"

    @test channels_support() == "<script src=\"/__/channels.js?v=$(Genie.Configuration.GENIE_VERSION)\"></script>"
    @test Genie.Router.routes()[1].path == "/__/channels.js"
    @test Genie.Router.channels()[1].path == "/__/unsubscribe"
    @test Genie.Router.channels()[2].path == "/__/subscribe"

    @test favicon_support() == "<link rel=\"icon\" type=\"image/x-icon\" href=\"/favicon.ico\" />"
  end

end;
1 Answers

Because @safetestset creates a new module for each TestSet, each one will need the using statements in the same way that a module does.

It is designed to isolate the TestSets and therefore by design each statement needs to import the modules that it uses.

From how I've seen other people use @safetestset (e.g. https://github.com/SciML/DifferentialEquations.jl/tree/master/test), typically @safetestset wraps round an include to a script and ensures that that script is run in isolation, avoiding problems like https://discourse.julialang.org/t/my-unit-tests-are-interferring-with-each-other/21686.

Related