New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shared code #11
Comments
Quick brain dump on my current thinking for this: Test snippetThe first case is simply code that we want to reuse at the global scope inside a @testsnippet Foo begin
x = "Something"
y = x * "And"
end
@testitem "Bar" begin
@includets Foo
@test x != y
end The test item @testitem "Bar" begin
x = "Something"
y = x * "And"
@test x != y
end The main open question here is how we handle namespace issues... Is there one flat namespace per package and all the Shared stateThis is trickier, and generally is more in the spirit of the setup and teardown methods in something like NUnit. The problem it attempts to solve is that in some cases it can be useful to generate some state and then reuse that state for the execution of multiple I think there are multiple ways we could solve this, here I lay out two. Test moduleThe idea around test modules would work like this: @testmodule Foo begin
x = load_large_data()
end
@testitem "Something" begin
@usingts Foo
@test bar(Foo.x) == "Something"
end So We could also show all the currently loaded test modules in the Julia Workspace, with an option to terminate them invidiually. Open question is again the namespace issue. Another question is how code changes would be detected, my best guess is that we could resolve that pretty easily with Revise.jl, though. Explicit setup and teardown with global data storageAnother option might be to have some explicit setup (and maybe teardown) methods. Things might look like this: @testsetup Foo begin
x = load_large_data()
@ts_store_data :foo_data x # Here we store x under the name :foo for later use by `@testitem`s
end
@testitem "Something" setup=[Foo] begin
x = @ts_load :foo
...
end So here we would have macros |
The test module alternative, if it can follow the same syntax of |
One other thing to consider: if tests allow grouping within a file, along the discussion in #5, the testsetup could be per-test-group. |
I think pytest is in general a good model to take inspiration/ideas from. In this case, shared code is handled by pytest "fixtures". These are basically functions that generate some object (data, function) that tests use by including it as an argument (test cases are functions in pytest). As a julia newbie, maybe let me describe how pytest works well for the present usecase: A fixture has a "scope" (function, class, module, package, session) that dictates how often it is invoked. The default is for every test (function), but you can also e.g. have one object that is reused by tests in a whole module, or once per test session. This is a really useful concept, as you can tailor your fixture to the scope you need, and not more. Setup/teardown is automatically handled by using the Python @pytest.fixture
def sending_user(mail_admin):
user = mail_admin.create_user()
yield user # -> at this point the test using the code gets the `user` object and runs
mail_admin.delete_user(user) I like that this is very natural, and you are using the same concept of a fixture function without having to learn any additional thing about setup/teardown. (in case you're not aware, the Fixtures can be cascaded/stacked, so you can neatly assemble hierarchies where one fixture uses another. (See above example, The one thing I don't like about pytest fixtures is that their discovery is a bit "magical" -- they are typically defined in a |
I just stumbled upon The usecase: Test integration with Pluto.jl More concretely, Pluto is sometimes used instead of an IDE to code julia files. I myself like it too. There is even The problem with
|
I just thought about a complete alternative approach. One could support a This would have the benefit that no extra name or tagging is needed. (EDIT: Of course, both |
@davidanthoff not sure about your timeline. In case you plan to make a new release soon, please take 5 minutes to read up on my thoughts above before releasing. |
I just realized that the @testsetup macro was already released (0.2.2 has tests for it) |
There will certainly be uses cases where one wants to use some test specific piece of code in more than one
@testitem
. The only way to deal with this at the moment is to put that code into its own .jl file and theninclude
that file from inside the@testitem
. While that works, there are probably better ways to organize this, and this issue is a place to discuss them.Broadly speaking there are probably different kind of cases: 1) function and type definitions that are meant to be used inside
@testitem
s, 2) initialization type code, 3) maybe more?Next major question is to what degree this kind of shared code should be handled by Revise vs just re-execution of a piece of code.
One idea for syntax could be something like a
@testmodule Foo begin end
for declaring a new module that can then be used viausing Foo
from inside a@testitem
, maybe... That probably would work quite well for the function and type definition case, but might not be the right model for init code?The text was updated successfully, but these errors were encountered: