|
| 1 | +# Advanced Usage |
| 2 | + |
| 3 | +```@meta |
| 4 | +CurrentModule = ParallelTestRunner |
| 5 | +DocTestSetup = quote |
| 6 | + using ParallelTestRunner |
| 7 | +end |
| 8 | +``` |
| 9 | + |
| 10 | +This page covers advanced features of `ParallelTestRunner` for customizing test execution. |
| 11 | + |
| 12 | +## Custom Test Suites |
| 13 | + |
| 14 | +By default, `runtests` automatically discovers all `.jl` files in your test directory. You can provide a custom test suite dictionary to have full control over which tests run: |
| 15 | + |
| 16 | +```julia |
| 17 | +using ParallelTestRunner |
| 18 | + |
| 19 | +# Manually define your test suite |
| 20 | +testsuite = Dict( |
| 21 | + "basic" => quote |
| 22 | + include("basic.jl") |
| 23 | + end, |
| 24 | + "advanced" => quote |
| 25 | + include("advanced.jl") |
| 26 | + end |
| 27 | +) |
| 28 | + |
| 29 | +runtests(MyPackage, ARGS; testsuite) |
| 30 | +``` |
| 31 | + |
| 32 | +Each value in the dictionary should be an expression (use `quote...end`) that executes the test code. |
| 33 | + |
| 34 | +## Filtering Tests |
| 35 | + |
| 36 | +You can use `find_tests` to automatically discover tests and then filter or modify them: |
| 37 | + |
| 38 | +```julia |
| 39 | +using ParallelTestRunner |
| 40 | + |
| 41 | +# Start with autodiscovered tests |
| 42 | +testsuite = find_tests(pwd()) |
| 43 | + |
| 44 | +# Parse arguments manually |
| 45 | +args = parse_args(ARGS) |
| 46 | + |
| 47 | +# Filter based on arguments |
| 48 | +if filter_tests!(testsuite, args) |
| 49 | + # Additional filtering is allowed |
| 50 | + # For example, remove platform-specific tests |
| 51 | + if Sys.iswindows() |
| 52 | + delete!(testsuite, "unix_only_test") |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +runtests(MyPackage, args; testsuite) |
| 57 | +``` |
| 58 | + |
| 59 | +The `filter_tests!` function returns `true` if no positional arguments were provided (allowing additional filtering) and `false` if the user specified specific tests (preventing further filtering). |
| 60 | + |
| 61 | +## Initialization Code |
| 62 | + |
| 63 | +Use the `init_code` keyword argument to provide code that runs before each test file. This is useful for: |
| 64 | +- Importing packages |
| 65 | +- Defining constants or helper functions |
| 66 | +- Setting up test infrastructure |
| 67 | + |
| 68 | +```julia |
| 69 | +using ParallelTestRunner |
| 70 | + |
| 71 | +const init_code = quote |
| 72 | + using Test |
| 73 | + using MyPackage |
| 74 | + |
| 75 | + # Define a helper function available to all tests |
| 76 | + function test_helper(x) |
| 77 | + return x * 2 |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +runtests(MyPackage, ARGS; init_code) |
| 82 | +``` |
| 83 | + |
| 84 | +The `init_code` is evaluated in each test's sandbox module, so all definitions are available to your test files. |
| 85 | + |
| 86 | +## Custom Workers |
| 87 | + |
| 88 | +For tests that require specific environment variables or Julia flags, you can use the `test_worker` keyword argument to assign tests to custom workers: |
| 89 | + |
| 90 | +```julia |
| 91 | +using ParallelTestRunner |
| 92 | + |
| 93 | +function test_worker(name) |
| 94 | + if name == "needs_env_var" |
| 95 | + # Create a worker with a specific environment variable |
| 96 | + return addworker(; env = ["SPECIAL_ENV_VAR" => "42"]) |
| 97 | + elseif name == "needs_threads" |
| 98 | + # Create a worker with multiple threads |
| 99 | + return addworker(; exeflags = ["--threads=4"]) |
| 100 | + end |
| 101 | + # Return nothing to use the default worker |
| 102 | + return nothing |
| 103 | +end |
| 104 | + |
| 105 | +testsuite = Dict( |
| 106 | + "needs_env_var" => quote |
| 107 | + @test ENV["SPECIAL_ENV_VAR"] == "42" |
| 108 | + end, |
| 109 | + "needs_threads" => quote |
| 110 | + @test Base.Threads.nthreads() == 4 |
| 111 | + end, |
| 112 | + "normal_test" => quote |
| 113 | + @test 1 + 1 == 2 |
| 114 | + end |
| 115 | +) |
| 116 | + |
| 117 | +runtests(MyPackage, ARGS; test_worker, testsuite) |
| 118 | +``` |
| 119 | + |
| 120 | +The `test_worker` function receives the test name and should return either: |
| 121 | +- A worker object (from `addworker`) for tests that need special configuration |
| 122 | +- `nothing` to use the default worker pool |
| 123 | + |
| 124 | +## Custom Output Streams |
| 125 | + |
| 126 | +You can redirect output to custom I/O streams: |
| 127 | + |
| 128 | +```julia |
| 129 | +using ParallelTestRunner |
| 130 | + |
| 131 | +io = IOBuffer() |
| 132 | +runtests(MyPackage, ARGS; stdout=io, stderr=io) |
| 133 | + |
| 134 | +# Process the output |
| 135 | +output = String(take!(io)) |
| 136 | +``` |
| 137 | + |
| 138 | +This is useful for: |
| 139 | +- Capturing test output for analysis |
| 140 | +- Writing to log files |
| 141 | +- Suppressing output in certain contexts |
| 142 | + |
| 143 | +## Custom Arguments |
| 144 | + |
| 145 | +If your package needs to accept its own command-line arguments in addition to `ParallelTestRunner`'s options, use `parse_args` with custom flags: |
| 146 | + |
| 147 | +```julia |
| 148 | +using ParallelTestRunner |
| 149 | + |
| 150 | +# Parse arguments with custom flags |
| 151 | +args = parse_args(ARGS; custom=["myflag", "another-flag"]) |
| 152 | + |
| 153 | +# Access custom flags |
| 154 | +if args.custom["myflag"] !== nothing |
| 155 | + println("Custom flag was set!") |
| 156 | +end |
| 157 | + |
| 158 | +# Pass parsed args to runtests |
| 159 | +runtests(MyPackage, args) |
| 160 | +``` |
| 161 | + |
| 162 | +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). |
| 163 | + |
| 164 | +## Manual Worker Management |
| 165 | + |
| 166 | +For advanced use cases, you can manually create workers: |
| 167 | + |
| 168 | +```julia |
| 169 | +using ParallelTestRunner |
| 170 | + |
| 171 | +# Add a single worker with custom configuration |
| 172 | +worker = addworker( |
| 173 | + env = ["CUSTOM_VAR" => "value"], |
| 174 | + exeflags = ["--check-bounds=no"] |
| 175 | +) |
| 176 | + |
| 177 | +# Add multiple workers |
| 178 | +workers = addworkers(4; env = ["THREADS" => "1"]) |
| 179 | +``` |
| 180 | + |
| 181 | +Workers created this way can be used with the `test_worker` function or for other distributed computing tasks. |
| 182 | + |
| 183 | +## Best Practices |
| 184 | + |
| 185 | +1. **Keep tests isolated**: Each test file runs in its own module, so avoid relying on global state between tests. |
| 186 | + |
| 187 | +2. **Use `init_code` for common setup**: Instead of duplicating setup code in each test file, use `init_code` to share common initialization. |
| 188 | + |
| 189 | +3. **Filter tests appropriately**: Use `filter_tests!` to respect user-specified test filters while allowing additional programmatic filtering. |
| 190 | + |
| 191 | +4. **Handle platform differences**: Use conditional logic in your test suite setup to handle platform-specific tests: |
| 192 | + |
| 193 | + ```julia |
| 194 | + testsuite = find_tests(pwd()) |
| 195 | + if Sys.iswindows() |
| 196 | + delete!(testsuite, "unix_specific_test") |
| 197 | + end |
| 198 | + ``` |
| 199 | + |
| 200 | +5. **Use custom workers sparingly**: Custom workers add overhead. Only use them when tests genuinely require different configurations. |
0 commit comments