Skip to content

Commit 7faa720

Browse files
authored
Merge pull request #273 from theogf/testset_name
Allow editing the testset name
2 parents 6925da1 + 2fd4943 commit 7faa720

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/testers.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ at input point `z` to confirm that there are correct `frule` and `rrule`s provid
1212
- `fdm`: the finite differencing method to use.
1313
- `fkwargs` are passed to `f` as keyword arguments.
1414
- If `check_inferred=true`, then the inferrability (type-stability) of the `frule` and `rrule` are checked.
15+
- `testset_name`: if provided, the name of the testset used to wrap the tests.
16+
Otherwise it is determined from the function and argument types.
1517
- All remaining keyword arguments are passed to `isapprox`.
1618
"""
17-
function test_scalar(f, z; rtol=1e-9, atol=1e-9, fdm=_fdm, fkwargs=NamedTuple(), check_inferred=true, kwargs...)
19+
function test_scalar(f, z; rtol=1e-9, atol=1e-9, fdm=_fdm, fkwargs=NamedTuple(), check_inferred=true, testset_name=nothing, kwargs...)
1820
# To simplify some of the calls we make later lets group the kwargs for reuse
1921
rule_test_kwargs = (; rtol=rtol, atol=atol, fdm=fdm, fkwargs=fkwargs, check_inferred=check_inferred, kwargs...)
2022
isapprox_kwargs = (; rtol=rtol, atol=atol, kwargs...)
23+
testset_name = isnothing(testset_name) ? "test_scalar: $f at $z" : testset_name
2124

22-
@testset "test_scalar: $f at $z" begin
25+
@testset "$(testset_name)" begin
2326
# z = x + im * y
2427
# Ω = u(x, y) + im * v(x, y)
2528
Ω = f(z; fkwargs...)
@@ -89,6 +92,8 @@ end
8992
- If `check_inferred=true`, then the inferrability (type-stability) of the `frule` is checked,
9093
as long as `f` is itself inferrable.
9194
- `fkwargs` are passed to `f` as keyword arguments.
95+
- `testset_name`: if provided, the name of the testset used to wrap the tests.
96+
Otherwise it is determined from the function and argument types.
9297
- All remaining keyword arguments are passed to `isapprox`.
9398
"""
9499
function test_frule(args...; kwargs...)
@@ -106,15 +111,16 @@ function test_frule(
106111
fkwargs::NamedTuple=NamedTuple(),
107112
rtol::Real=1e-9,
108113
atol::Real=1e-9,
114+
testset_name=nothing,
109115
kwargs...,
110116
)
111117
# To simplify some of the calls we make later lets group the kwargs for reuse
112118
isapprox_kwargs = (; rtol=rtol, atol=atol, kwargs...)
113-
119+
testset_name = isnothing(testset_name) ? "test_frule: $f on $(_string_typeof(args))" : testset_name
114120
# and define a helper closure
115121
call_on_copy(f, xs...) = deepcopy(f)(deepcopy(xs)...; deepcopy(fkwargs)...)
116122

117-
@testset "test_frule: $f on $(_string_typeof(args))" begin
123+
@testset "$(testset_name)" begin
118124

119125
primals_and_tangents = auto_primal_and_tangent.((f, args...))
120126
primals = primal.(primals_and_tangents)
@@ -164,6 +170,8 @@ end
164170
- If `check_inferred=true`, then the inferrability (type-stability) of the `rrule` is checked
165171
— if `f` is itself inferrable — along with the inferrability of the pullback it returns.
166172
- `fkwargs` are passed to `f` as keyword arguments.
173+
- `testset_name`: if provided, the name of the testset used to wrap the tests.
174+
Otherwise it is determined from the function and argument types.
167175
- All remaining keyword arguments are passed to `isapprox`.
168176
"""
169177
function test_rrule(args...; kwargs...)
@@ -182,15 +190,16 @@ function test_rrule(
182190
fkwargs::NamedTuple=NamedTuple(),
183191
rtol::Real=1e-9,
184192
atol::Real=1e-9,
193+
testset_name=nothing,
185194
kwargs...,
186195
)
187196
# To simplify some of the calls we make later lets group the kwargs for reuse
188197
isapprox_kwargs = (; rtol=rtol, atol=atol, kwargs...)
189-
198+
testset_name = isnothing(testset_name) ? "test_rrule: $f on $(_string_typeof(args))" : testset_name
190199
# and define helper closure over fkwargs
191200
call(f, xs...) = f(xs...; fkwargs...)
192201

193-
@testset "test_rrule: $f on $(_string_typeof(args))" begin
202+
@testset "$(testset_name)" begin
194203

195204
# Check correctness of evaluation.
196205
primals_and_tangents = auto_primal_and_tangent.((f, args...))

test/testers.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,4 +725,15 @@ end
725725
test_frule(f_vec_of_tuples, x_tuples)
726726
test_rrule(f_vec_of_tuples, x_tuples)
727727
end
728+
@testset "check passing of testset_name kwarg" begin
729+
name = "my test name"
730+
double(x) = 2x
731+
@scalar_rule(double(x), 2)
732+
x = test_scalar(double, 2.1; testset_name=name)
733+
@test x.description == name
734+
x = test_frule(identity, 1.0; testset_name=name)
735+
@test x.description == name
736+
x = test_rrule(identity, 1.0; testset_name=name)
737+
@test x.description == name
738+
end
728739
end

0 commit comments

Comments
 (0)