-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from daniel-thom/retest
Use ReTest.jl for tests
- Loading branch information
Showing
10 changed files
with
161 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,70 @@ | ||
# Running Tests | ||
|
||
## Standard test execution | ||
Unit tests can be executed in the REPL by executing the following: | ||
|
||
```julia | ||
julia> ] test | ||
``` | ||
|
||
The unit test module supports several customizations to aid development and | ||
debug. For instance, runnning a specific test file | ||
## Interactive test execution | ||
While developing code and tests it can be convenient to run a subset of tests. | ||
You can do this with a combination of TestEnv.jl and ReTest.jl. | ||
|
||
- Run a subset of tests in the REPL: | ||
**Note**: Per recommendations from the developers of TestEnv.jl, install the package | ||
in your global julia environment. Do the same for Revise.jl. | ||
|
||
``` | ||
$ julia | ||
julia> ] | ||
(@v1.10) pkg> add TestEnv Revise | ||
``` | ||
|
||
Start the environment with the InfrastructureSystems.jl environment. | ||
``` | ||
$ julia --project | ||
``` | ||
|
||
Load the test environment. | ||
``` | ||
julia> using TestEnv | ||
julia> TestEnv.activate() | ||
``` | ||
|
||
Load the tests through ReTest.jl and Revise.jl. | ||
```julia | ||
julia> push!(ARGS, "<test_filename_without_.jl>") | ||
julia> include("test/runtests.jl") | ||
julia> include("test/load_tests.jl") | ||
``` | ||
|
||
- Change logging level(s): | ||
Run all tests. | ||
```julia | ||
julia> run_tests() | ||
``` | ||
|
||
Run a subset of tests with a regular expression. This pattern matches multiple testset definitions. | ||
The `run_tests` function forwards all arguments and keyword arguments to `ReTest.retest`. | ||
``` | ||
julia> run_tests(r"Test.*components") | ||
``` | ||
|
||
Refer to the [ReTest documentation](https://juliatesting.github.io/ReTest.jl/stable/) for more | ||
information. | ||
|
||
## Change logging levels | ||
|
||
```julia | ||
julia> InfrastructureSystems.make_logging_config_file("logging_config.toml") | ||
julia> ENV["SIENNA_LOGGING_CONFIG"] = "logging_config.toml" | ||
``` | ||
|
||
Edit the file to suit your preferences and rerun. | ||
```julia | ||
julia> IS.make_logging_config_file("logging_config.toml") | ||
julia> ENV["SIIP_LOGGING_CONFIG"] = "logging_config.toml" | ||
# Edit the file to suit your preferences. | ||
julia> include("test/runtests.jl") | ||
julia> run_tests() | ||
``` | ||
|
||
**Note** that you can filter out noisy log groups in this file. | ||
|
||
## Noisy log messages | ||
The unit test module appends a summary of all log message counts to the log | ||
file. If a message is logged too frequently then consider tagging that message | ||
with maxlog=X to suppress it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module InfrastructureSystemsTests | ||
|
||
using ReTest | ||
using Logging | ||
import Dates | ||
import TerminalLoggers: TerminalLogger | ||
import TimeSeries | ||
import UUIDs | ||
import JSON3 | ||
import HDF5 | ||
using DataStructures: SortedDict | ||
using DataFrames | ||
using Random | ||
using ProgressLogging | ||
|
||
import InfrastructureSystems | ||
|
||
import Aqua | ||
Aqua.test_unbound_args(InfrastructureSystems) | ||
Aqua.test_undefined_exports(InfrastructureSystems) | ||
Aqua.test_ambiguities(InfrastructureSystems) | ||
Aqua.test_stale_deps(InfrastructureSystems) | ||
Aqua.test_deps_compat(InfrastructureSystems) | ||
|
||
const IS = InfrastructureSystems | ||
const BASE_DIR = | ||
abspath(joinpath(dirname(Base.find_package("InfrastructureSystems")), "..")) | ||
const DATA_DIR = joinpath(BASE_DIR, "test", "data") | ||
const FORECASTS_DIR = joinpath(DATA_DIR, "time_series") | ||
|
||
const LOG_FILE = "infrastructure-systems.log" | ||
|
||
include("common.jl") | ||
|
||
for filename in readdir(joinpath(BASE_DIR, "test")) | ||
if startswith(filename, "test_") && endswith(filename, ".jl") | ||
include(filename) | ||
end | ||
end | ||
|
||
function get_logging_level_from_env(env_name::String, default) | ||
level = get(ENV, env_name, default) | ||
return IS.get_logging_level(level) | ||
end | ||
|
||
function run_tests(args...; kwargs...) | ||
logger = global_logger() | ||
try | ||
logging_config_filename = get(ENV, "SIENNA_LOGGING_CONFIG", nothing) | ||
if logging_config_filename !== nothing | ||
config = IS.LoggingConfiguration(logging_config_filename) | ||
else | ||
config = IS.LoggingConfiguration(; | ||
filename = LOG_FILE, | ||
file_level = get_logging_level_from_env("SIENNA_FILE_LOG_LEVEL", "Info"), | ||
console_level = get_logging_level_from_env( | ||
"SIENNA_CONSOLE_LOG_LEVEL", | ||
"Error", | ||
), | ||
) | ||
end | ||
console_logger = TerminalLogger(config.console_stream, config.console_level) | ||
|
||
IS.open_file_logger(config.filename, config.file_level) do file_logger | ||
levels = (Logging.Info, Logging.Warn, Logging.Error) | ||
multi_logger = | ||
IS.MultiLogger([console_logger, file_logger], IS.LogEventTracker(levels)) | ||
global_logger(multi_logger) | ||
|
||
if !isempty(config.group_levels) | ||
IS.set_group_levels!(multi_logger, config.group_levels) | ||
end | ||
|
||
@time retest(args...; kwargs...) | ||
@test length(IS.get_log_events(multi_logger.tracker, Logging.Error)) == 0 | ||
@info IS.report_log_summary(multi_logger) | ||
end | ||
finally | ||
# Guarantee that the global logger is reset. | ||
global_logger(logger) | ||
nothing | ||
end | ||
end | ||
|
||
export run_tests | ||
|
||
end | ||
|
||
using .InfrastructureSystemsTests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Revise | ||
|
||
# Copied from https://juliatesting.github.io/ReTest.jl/stable/#Working-with-Revise | ||
function recursive_includet(filename) | ||
already_included = copy(Revise.included_files) | ||
includet(filename) | ||
newly_included = setdiff(Revise.included_files, already_included) | ||
for (mod, file) in newly_included | ||
Revise.track(mod, file) | ||
end | ||
end | ||
|
||
recursive_includet("InfrastructureSystemsTests.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,4 @@ | ||
using Test | ||
using Logging | ||
import Dates | ||
import TerminalLoggers: TerminalLogger | ||
import TimeSeries | ||
import UUIDs | ||
import JSON3 | ||
import HDF5 | ||
using DataStructures: SortedDict | ||
using DataFrames | ||
using Random | ||
using ProgressLogging | ||
using InfrastructureSystems | ||
|
||
import InfrastructureSystems | ||
|
||
import Aqua | ||
Aqua.test_unbound_args(InfrastructureSystems) | ||
Aqua.test_undefined_exports(InfrastructureSystems) | ||
Aqua.test_ambiguities(InfrastructureSystems) | ||
Aqua.test_stale_deps(InfrastructureSystems) | ||
Aqua.test_deps_compat(InfrastructureSystems) | ||
|
||
const IS = InfrastructureSystems | ||
const BASE_DIR = | ||
abspath(joinpath(dirname(Base.find_package("InfrastructureSystems")), "..")) | ||
const DATA_DIR = joinpath(BASE_DIR, "test", "data") | ||
const FORECASTS_DIR = joinpath(DATA_DIR, "time_series") | ||
|
||
const LOG_FILE = "infrastructure-systems.log" | ||
|
||
include("common.jl") | ||
|
||
""" | ||
Copied @includetests from https://github.com/ssfrr/TestSetExtensions.jl. | ||
Ideally, we could import and use TestSetExtensions. Its functionality was broken by changes | ||
in Julia v0.7. Refer to https://github.com/ssfrr/TestSetExtensions.jl/pull/7. | ||
""" | ||
|
||
""" | ||
Includes the given test files, given as a list without their ".jl" extensions. | ||
If none are given it will scan the directory of the calling file and include all | ||
the julia files. | ||
""" | ||
macro includetests(testarg...) | ||
if length(testarg) == 0 | ||
tests = [] | ||
elseif length(testarg) == 1 | ||
tests = testarg[1] | ||
else | ||
error("@includetests takes zero or one argument") | ||
end | ||
|
||
quote | ||
tests = $tests | ||
rootfile = @__FILE__ | ||
if length(tests) == 0 | ||
tests = readdir(dirname(rootfile)) | ||
tests = filter( | ||
f -> | ||
startswith(f, "test_") && endswith(f, ".jl") && f != basename(rootfile), | ||
tests, | ||
) | ||
else | ||
tests = map(f -> string(f, ".jl"), tests) | ||
end | ||
println() | ||
for test in tests | ||
print(splitext(test)[1], ": ") | ||
include(test) | ||
println() | ||
end | ||
end | ||
end | ||
|
||
function get_logging_level_from_env(env_name::String, default) | ||
level = get(ENV, env_name, default) | ||
return IS.get_logging_level(level) | ||
end | ||
|
||
function run_tests() | ||
logging_config_filename = get(ENV, "SIIP_LOGGING_CONFIG", nothing) | ||
if logging_config_filename !== nothing | ||
config = IS.LoggingConfiguration(logging_config_filename) | ||
else | ||
config = IS.LoggingConfiguration(; | ||
filename = LOG_FILE, | ||
file_level = Logging.Info, | ||
console_level = Logging.Error, | ||
) | ||
end | ||
console_logger = TerminalLogger(config.console_stream, config.console_level) | ||
|
||
IS.open_file_logger(config.filename, config.file_level) do file_logger | ||
levels = (Logging.Info, Logging.Warn, Logging.Error) | ||
multi_logger = | ||
IS.MultiLogger([console_logger, file_logger], IS.LogEventTracker(levels)) | ||
global_logger(multi_logger) | ||
|
||
if !isempty(config.group_levels) | ||
IS.set_group_levels!(multi_logger, config.group_levels) | ||
end | ||
|
||
@time @testset "Begin Systems tests" begin | ||
@includetests ARGS | ||
end | ||
|
||
@test length(IS.get_log_events(multi_logger.tracker, Logging.Error)) == 0 | ||
|
||
@info IS.report_log_summary(multi_logger) | ||
end | ||
end | ||
|
||
logger = global_logger() | ||
|
||
try | ||
run_tests() | ||
finally | ||
# Guarantee that the global logger is reset. | ||
global_logger(logger) | ||
nothing | ||
end | ||
include("InfrastructureSystemsTests.jl") | ||
run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters