@@ -2,14 +2,107 @@ module TestItems
22
33export @testitem , @testmodule , @testsnippet
44
5+ """
6+ @testitem "name" [keyword_args...] begin
7+ # test code
8+ end
9+
10+ Define a test item, a self-contained unit of test code.
11+
12+ Each test item is run in its own `module`, so test items cannot share state
13+ with each other directly. Use `@testmodule` or `@testsnippet` with the `setup` keyword
14+ to share setup code between test items.
15+
16+ ## Keyword arguments
17+
18+ - `tags::Vector{Symbol}`: Tags for filtering which test items to run.
19+ Example: `tags=[:slow, :integration]`.
20+ - `default_imports::Bool`: Whether to automatically `using Test` and `using <PackageName>`
21+ before running the test code. Defaults to `true`.
22+ - `setup::Vector{Symbol}`: Names of `@testmodule` or `@testsnippet` definitions to
23+ evaluate before the test item runs.
24+
25+ ## Examples
26+
27+ ```julia
28+ @testitem "basic addition" begin
29+ @test 1 + 1 == 2
30+ end
31+
32+ @testitem "no default imports" default_imports=false begin
33+ using Test
34+ @test true
35+ end
36+
37+ @testitem "with setup" tags=[:integration] setup=[DatabaseHelper] begin
38+ db = DatabaseHelper.connect()
39+ @test isopen(db)
40+ end
41+ ```
42+ """
543macro testitem (ex... )
644 return nothing
745end
846
47+ """
48+ @testmodule ModuleName begin
49+ # module body
50+ end
51+
52+ Define a reusable test setup module that can be shared across test items via the
53+ `setup` keyword of `@testitem`.
54+
55+ A `@testmodule` is evaluated once per test process and the resulting module is made
56+ available to every test item that lists it in `setup`. This makes it suitable for
57+ expensive setup like establishing database connections or loading large datasets, since
58+ the work is done only once and then reused.
59+
60+ ## Examples
61+
62+ ```julia
63+ @testmodule DatabaseHelper begin
64+ using SomeDBPackage
65+ const DB_URL = "localhost:5432/testdb"
66+ connect() = SomeDBPackage.connect(DB_URL)
67+ end
68+
69+ @testitem "query works" setup=[DatabaseHelper] begin
70+ db = DatabaseHelper.connect()
71+ @test isopen(db)
72+ end
73+ ```
74+ """
975macro testmodule (ex... )
1076 return nothing
1177end
1278
79+ """
80+ @testsnippet SnippetName begin
81+ # setup code
82+ end
83+
84+ Define a reusable test setup snippet that can be shared across test items via the
85+ `setup` keyword of `@testitem`.
86+
87+ Unlike `@testmodule`, a `@testsnippet`'s code is evaluated directly in the scope of
88+ each test item that references it, rather than in its own module. This means any
89+ variables or functions defined in the snippet are available directly without module
90+ qualification. This makes snippets convenient for small bits of shared setup code.
91+
92+ ## Examples
93+
94+ ```julia
95+ @testsnippet CommonSetup begin
96+ x = 42
97+ helper() = "hello"
98+ end
99+
100+ @testitem "use snippet" setup=[CommonSetup] begin
101+ @test x == 42
102+ @test helper() == "hello"
103+ end
104+ ```
105+ """
13106macro testsnippet (ex... )
14107 return nothing
15108end
0 commit comments