Skip to content

Commit f67b202

Browse files
committed
[docs] Execute examples when building the docs
1 parent 7e025e5 commit f67b202

File tree

9 files changed

+105
-37
lines changed

9 files changed

+105
-37
lines changed

docs/MyPackage/Project.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name = "MyPackage"
2+
uuid = "0b48ad8b-a282-4514-80d9-bf0add6b76ed"
3+
4+
[deps]
5+
ParallelTestRunner = "d3525ed8-44d0-4b2c-a655-542cee43accc"
6+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
7+
8+
[sources]
9+
ParallelTestRunner = {path = ".."}
10+
11+
[compat]
12+
julia = "1.10"

docs/MyPackage/src/MyPackage.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module MyPackage
2+
end # module MyPackage

docs/MyPackage/test/advanced.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using Test
2+
3+
@test true

docs/MyPackage/test/basic.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using Test
2+
3+
@test true

docs/MyPackage/test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
using MyPackage
2+
using ParallelTestRunner

docs/Project.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
MyPackage = "0b48ad8b-a282-4514-80d9-bf0add6b76ed"
34
ParallelTestRunner = "d3525ed8-44d0-4b2c-a655-542cee43accc"
4-
5-
[compat]
6-
Documenter = "1"
5+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
76

87
[sources]
8+
MyPackage = {path = "MyPackage"}
99
ParallelTestRunner = {path = ".."}
10+
11+
[compat]
12+
Documenter = "1"

docs/src/advanced.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
```@setup mypackage
2+
using ParallelTestRunner
3+
mypackage_dir = joinpath(pkgdir(ParallelTestRunner), "docs", "MyPackage")
4+
using MyPackage
5+
test_dir = joinpath(mypackage_dir, "test")
6+
```
7+
18
# Advanced Usage
29

310
```@meta
@@ -14,40 +21,51 @@ This page covers advanced features of `ParallelTestRunner` for customizing test
1421
By default, [`runtests`](@ref) automatically discovers all `.jl` files in your `test/` directory (excluding `runtests.jl` itself) using the `find_tests` function.
1522
You can customize which tests to run by providing a custom `testsuite` dictionary:
1623

17-
```julia
24+
```@example mypackage
25+
using ParallelTestRunner
26+
using MyPackage
27+
1828
# Manually define your test suite
1929
testsuite = Dict(
2030
"basic" => quote
2131
include("basic.jl")
2232
end,
2333
"advanced" => quote
2434
include("advanced.jl")
35+
@test 40 + 2 ≈ 42
2536
end
2637
)
2738
28-
runtests(MyModule, ARGS; testsuite)
39+
cd(test_dir) do # hide
40+
runtests(MyPackage, ARGS; testsuite)
41+
end # hide
2942
```
3043

3144
## Filtering Test Files
3245

3346
You can also use [`find_tests`](@ref) to automatically discover test files and then filter or modify them.
3447
This requires manually parsing arguments so that filtering is only applied when the user did not request specific tests to run:
3548

36-
```julia
49+
```@example mypackage
50+
using ParallelTestRunner
51+
using MyPackage
52+
3753
# Start with autodiscovered tests
54+
cd(test_dir) do # hide
3855
testsuite = find_tests(pwd())
3956
4057
# Parse arguments
4158
args = parse_args(ARGS)
4259
4360
if filter_tests!(testsuite, args)
44-
# Remove tests that shouldn't run on Windows
45-
if Sys.iswindows()
46-
delete!(testsuite, "ext/specialfunctions")
61+
# Remove tests that shouldn't run on non-Windows systems
62+
if !Sys.iswindows()
63+
delete!(testsuite, "advanced")
4764
end
4865
end
4966
50-
runtests(MyModule, args; testsuite)
67+
runtests(MyPackage, args; testsuite)
68+
end # hide
5169
```
5270

5371
The [`filter_tests!`](@ref) function returns `true` if no positional arguments were provided (allowing additional filtering) and `false` if the user specified specific tests (preventing further filtering).
@@ -60,20 +78,20 @@ This is useful for:
6078
- Defining constants, defaults or helper functions
6179
- Setting up test infrastructure
6280

63-
```julia
81+
```@example mypackage
6482
using ParallelTestRunner
83+
using MyPackage
6584
6685
const init_code = quote
67-
using Test
68-
using MyPackage
69-
7086
# Define a helper function available to all tests
7187
function test_helper(x)
7288
return x * 2
7389
end
7490
end
7591
92+
cd(test_dir) do # hide
7693
runtests(MyPackage, ARGS; init_code)
94+
end # hide
7795
```
7896

7997
The `init_code` is evaluated in each test's sandbox module, so all definitions are available to your test files.
@@ -82,8 +100,9 @@ The `init_code` is evaluated in each test's sandbox module, so all definitions a
82100

83101
For tests that require specific environment variables or Julia flags, you can use the `test_worker` keyword argument to [`runtests`](@ref) to assign tests to custom workers:
84102

85-
```julia
103+
```@example mypackage
86104
using ParallelTestRunner
105+
using MyPackage
87106
88107
function test_worker(name)
89108
if name == "needs_env_var"
@@ -120,8 +139,9 @@ The `test_worker` function receives the test name and should return either:
120139

121140
If your package needs to accept its own command-line arguments in addition to `ParallelTestRunner`'s options, use [`parse_args`](@ref) with custom flags:
122141

123-
```julia
142+
```@example mypackage
124143
using ParallelTestRunner
144+
using MyPackage
125145
126146
# Parse arguments with custom flags
127147
args = parse_args(ARGS; custom=["myflag", "another-flag"])
@@ -132,10 +152,12 @@ if args.custom["myflag"] !== nothing
132152
end
133153
134154
# Pass parsed args to runtests
155+
cd(test_dir) do # hide
135156
runtests(MyPackage, args)
157+
end # hide
136158
```
137159

138-
Custom flags are stored in the `custom` field of the `ParsedArgs` object, with values of `nothing` (not set) or `Some(value)` (set, with optional value).
160+
Custom flags are stored in the `custom` field of the [`ParsedArgs`](@ref) object, with values of `nothing` (not set) or `Some(value)` (set, with optional value).
139161

140162
## Interactive use
141163

@@ -146,11 +168,11 @@ For example, here is how we could run the subset of test files that start with t
146168
# Start julia in an environment where `MyPackage.jl` is available
147169
julia --project
148170
```
149-
```julia-repl
150-
julia> using Pkg
171+
```@repl mypackage
172+
using Pkg
151173
152174
# No need to start a fresh session to change threading
153-
julia> Pkg.test("MyModule"; test_args=`--verbose test_cool_feature`, julia_args=`--threads=auto`);
175+
Pkg.test("MyPackage"; test_args=`--verbose advanced`, julia_args=`--threads=auto`);
154176
```
155177

156178
Alternatively, arguments can be passed directly from the command line with a shell alias like the one below:

docs/src/index.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ DocTestSetup = quote
77
end
88
```
99

10+
```@setup mypackage
11+
using ParallelTestRunner
12+
mypackage_dir = joinpath(pkgdir(ParallelTestRunner), "docs", "MyPackage")
13+
using MyPackage
14+
test_dir = joinpath(mypackage_dir, "test")
15+
```
16+
1017
[ParallelTestRunner.jl](https://github.com/JuliaTesting/ParallelTestRunner.jl) is a simple parallel test runner for Julia tests with automatic test discovery.
1118
It runs each test file concurrently in isolated worker processes, providing real-time progress output and efficient resource management.
1219

@@ -19,11 +26,13 @@ It runs each test file concurrently in isolated worker processes, providing real
1926

2027
2. **Update your `test/runtests.jl`**:
2128

22-
```julia
29+
```@example mypackage
2330
using MyPackage
2431
using ParallelTestRunner
2532
33+
cd(test_dir) do # hide
2634
runtests(MyPackage, ARGS)
35+
end # hide
2736
```
2837

2938
That's it! `ParallelTestRunner` will automatically:
@@ -108,14 +117,6 @@ The test runner provides real-time output showing:
108117
- Memory allocation
109118
- RSS (Resident Set Size) memory usage
110119

111-
Example output:
112-
113-
```
114-
Test | Time (s) | GC (s) | GC % | Alloc (MB) | RSS (MB) |
115-
basic (1) | 0.12 | 0.01 | 8.3 | 5.23 | 125.45 |
116-
integration (2) | 2.45 | 0.15 | 6.1 | 45.67 | 234.12 |
117-
```
118-
119120
### Graceful Interruption
120121

121122
Press `Ctrl+C` to interrupt the test run. The framework will:

src/ParallelTestRunner.jl

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -679,29 +679,49 @@ Several keyword arguments are also supported:
679679
680680
## Examples
681681
682+
Run all tests with default settings (auto-discovers `.jl` files)
683+
684+
```julia
685+
using ParallelTestRunner
686+
using MyPackage
687+
688+
runtests(MyPackage, ARGS)
689+
```
690+
691+
Run only tests matching "integration" (matched with `startswith`):
682692
```julia
683-
# Run all tests with default settings (auto-discovers .jl files)
684-
runtests(MyModule, ARGS)
693+
using ParallelTestRunner
694+
using MyPackage
695+
696+
runtests(MyPackage, ["integration"])
697+
```
685698
686-
# Run only tests matching "integration"
687-
runtests(MyModule, ["integration"])
699+
Define a custom test suite
700+
```julia
701+
using ParallelTestRunner
702+
using MyPackage
688703
689-
# Define a custom test suite
690704
testsuite = Dict(
691705
"custom" => quote
692706
@test 1 + 1 == 2
693707
end
694708
)
695-
runtests(MyModule, ARGS; testsuite)
696709
697-
# Customize the test suite
710+
runtests(MyPackage, ARGS; testsuite)
711+
```
712+
713+
Customize the test suite
714+
```julia
715+
using ParallelTestRunner
716+
using MyPackage
717+
698718
testsuite = find_tests(pwd())
699719
args = parse_args(ARGS)
700720
if filter_tests!(testsuite, args)
701721
# Remove a specific test
702722
delete!(testsuite, "slow_test")
703723
end
704-
runtests(MyModule, args; testsuite)
724+
runtests(MyPackage, args; testsuite)
705725
```
706726
707727
## Memory Management

0 commit comments

Comments
 (0)