Skip to content

Truly independent web browser

LibTest: Run combined source suites in isolated processes

Timothy Flynn711c68c7d3ab
diff --git a/Documentation/Testing.md b/Documentation/Testing.md
index 925dcac0ac4..94e432ff355 100644
--- a/Documentation/Testing.md
+++ b/Documentation/Testing.md
@@ -48,6 +48,24 @@ CTEST_OUTPUT_ON_FAILURE=1 ninja test
ctest --output-on-failure
```
+### C++ unit tests
+
+Ordinary unit tests share one executable per library. Each original test source runs in a separate
+process so its setup function and process-wide state remain isolated. Dedicated runners such as
+`test-js` and `test-web`, custom event-loop runners, and incompatible Rust panic fixtures remain separate.
+
+LibTest executables accept `--list`, an optional case-name pattern, and `--suite` to select a suite
+by its source file name without the extension:
+
+```sh
+cmake --build Build/release --target TestLibTest
+Build/release/bin/TestLibTest --list
+Build/release/bin/TestLibTest --suite TestGenerator
+Build/release/bin/TestLibTest 'TestGenerator::*'
+```
+
+Build the library's executable to link its sources, and use `--suite` to select the tests from a source file.
+
### JavaScript tests
Build `test-js` to build the runtime runner and `js`, and prepare the shared
diff --git a/Libraries/LibTest/TestCase.h b/Libraries/LibTest/TestCase.h
index b5c82c973f5..7c9724a20e7 100644
--- a/Libraries/LibTest/TestCase.h
+++ b/Libraries/LibTest/TestCase.h
@@ -32,18 +32,20 @@ inline void run_with_randomness_source(Randomized::RandomnessSource source, Test
class TEST_API TestCase : public RefCounted<TestCase> {
public:
- TestCase(ByteString const& name, TestFunction&& fn, bool is_benchmark)
- : m_name(name)
- , m_function(move(fn))
+ TestCase(ByteString name, ByteString source, TestFunction&& function, bool is_benchmark)
+ : m_name(move(name))
+ , m_source(move(source))
+ , m_function(move(function))
, m_is_benchmark(is_benchmark)
{
}
bool is_benchmark() const { return m_is_benchmark; }
ByteString const& name() const { return m_name; }
+ ByteString const& source() const { return m_source; }
TestFunction const& func() const { return m_function; }
- static NonnullRefPtr<TestCase> randomized(ByteString const& name, TestFunction&& test_function)
+ static NonnullRefPtr<TestCase> randomized(ByteString name, ByteString source, TestFunction&& test_function)
{
using namespace Randomized;
@@ -98,82 +100,54 @@ public:
}
// All randomized_runs() values generated + passed the test.
};
- return make_ref_counted<TestCase>(name, move(test_case_function), false);
+ return make_ref_counted<TestCase>(move(name), move(source), move(test_case_function), false);
}
private:
ByteString m_name;
+ ByteString m_source;
TestFunction m_function;
bool m_is_benchmark;
};
// Helper to hide implementation of TestSuite from users
TEST_API void add_test_case_to_suite(NonnullRefPtr<TestCase> const& test_case);
-TEST_API void set_suite_setup_function(Function<void()> setup);
+TEST_API void set_suite_setup_function(ByteString const& source, Function<void()> setup);
}
-#define TEST_SETUP \
- static void __setup(); \
- struct __setup_type { \
- __setup_type() \
- { \
- Test::set_suite_setup_function(__setup); \
- } \
- }; \
- static struct __setup_type __setup_type; \
+#define TEST_SETUP \
+ static void __setup(); \
+ [[maybe_unused]] static auto const __setup_registration = [] { \
+ ::Test::set_suite_setup_function(__FILE__, __setup); \
+ return 0; \
+ }(); \
static void __setup()
-// Unit test
-
-#define __TESTCASE_FUNC(x) __test_##x
-#define __TESTCASE_TYPE(x) __TestCase_##x
-
-#define TEST_CASE(x) \
- static void __TESTCASE_FUNC(x)(); \
- struct __TESTCASE_TYPE(x) { \
- __TESTCASE_TYPE(x) \
- () \
- { \
- add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(#x, __TESTCASE_FUNC(x), false))); \
- } \
- }; \
- static struct __TESTCASE_TYPE(x) __TESTCASE_TYPE(x); \
- static void __TESTCASE_FUNC(x)()
-
-// Benchmark
-
-#define __BENCHMARK_FUNC(x) __benchmark_##x
-#define __BENCHMARK_TYPE(x) __BenchmarkCase_##x
-
-#define BENCHMARK_CASE(x) \
- static void __BENCHMARK_FUNC(x)(); \
- struct __BENCHMARK_TYPE(x) { \
- __BENCHMARK_TYPE(x) \
- () \
- { \
- add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(#x, __BENCHMARK_FUNC(x), true))); \
- } \
- }; \
- static struct __BENCHMARK_TYPE(x) __BENCHMARK_TYPE(x); \
- static void __BENCHMARK_FUNC(x)()
-
-// Randomized test
-
-#define __RANDOMIZED_TEST_FUNC(x) __randomized_test_##x
-#define __RANDOMIZED_TEST_TYPE(x) __RandomizedTestCase_##x
-
-#define RANDOMIZED_TEST_CASE(x) \
- static void __RANDOMIZED_TEST_FUNC(x)(); \
- struct __RANDOMIZED_TEST_TYPE(x) { \
- __RANDOMIZED_TEST_TYPE(x) \
- () \
- { \
- add_test_case_to_suite(::Test::TestCase::randomized(#x, __RANDOMIZED_TEST_FUNC(x))); \
- } \
- }; \
- static struct __RANDOMIZED_TEST_TYPE(x) __RANDOMIZED_TEST_TYPE(x); \
- static void __RANDOMIZED_TEST_FUNC(x)()
+// Registrations have internal linkage so different source files may use the same case names.
+#define TEST_CASE(x) \
+ static void __test_##x(); \
+ [[maybe_unused]] static auto const __test_registration_##x = [] { \
+ ::Test::add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(#x, __FILE__, __test_##x, false))); \
+ return 0; \
+ }(); \
+ static void __test_##x()
+
+#define BENCHMARK_CASE(x) \
+ static void __benchmark_##x(); \
+ [[maybe_unused]] static auto const __benchmark_registration_##x = [] { \
+ ::Test::add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(#x, __FILE__, __benchmark_##x, true))); \
+ return 0; \
+ }(); \
+ static void __benchmark_##x()
+
+#define RANDOMIZED_TEST_CASE(x) \
+ static void __randomized_test_##x(); \
+ [[maybe_unused]] static auto const __randomized_registration_##x = [] { \
+ ::Test::add_test_case_to_suite(::Test::TestCase::randomized(#x, __FILE__, __randomized_test_##x)); \
+ return 0; \
+ }(); \
+ static void __randomized_test_##x()
// This allows us to print the generated locals in the test after a failure is fully shrunk.
#define GEN(identifier, value) \
diff --git a/Libraries/LibTest/TestSuite.cpp b/Libraries/LibTest/TestSuite.cpp
index 307ef4b57f8..0ac969e5c1a 100644
--- a/Libraries/LibTest/TestSuite.cpp
+++ b/Libraries/LibTest/TestSuite.cpp
@@ -6,8 +6,11 @@
*/
#include <AK/Function.h>
+#include <AK/LexicalPath.h>
#include <AK/Time.h>
#include <LibCore/ArgsParser.h>
+#include <LibCore/Process.h>
+#include <LibCore/System.h>
#include <LibTest/Macros.h>
#include <LibTest/TestResult.h>
#include <LibTest/TestSuite.h>
@@ -69,9 +72,9 @@ void add_test_case_to_suite(NonnullRefPtr<TestCase> const& test_case)
}
// Declared in TestCase.h
-void set_suite_setup_function(Function<void()> setup)
+void set_suite_setup_function(ByteString const& source, Function<void()> setup)
{
- TestSuite::the().set_suite_setup(move(setup));
+ TestSuite::the().set_suite_setup(source, move(setup));
}
// Declared in Macros.h
@@ -120,38 +123,81 @@ int TestSuite::main(ByteString const& suite_name, Span<StringView> arguments)
bool do_benchmarks_only = false;
bool do_list_cases = false;
StringView search_string = "*"sv;
+ StringView selected_suite;
args_parser.add_option(do_tests_only, "Only run tests.", "tests");
args_parser.add_option(do_benchmarks_only, "Only run benchmarks.", "bench");
args_parser.add_option(m_benchmark_repetitions, "Number of times to repeat each benchmark (default 1)", "benchmark_repetitions", 0, "N");
args_parser.add_option(m_randomized_runs, "Number of times to run each RANDOMIZED_TEST_CASE (default 100)", "randomized_runs", 0, "RUNS");
args_parser.add_option(do_list_cases, "List available test cases.", "list");
+ args_parser.add_option(selected_suite, "Only run cases from this suite.", "suite", 0, "SUITE");
args_parser.add_positional_argument(search_string, "Only run matching cases.", "pattern", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
- if (m_setup)
- m_setup();
-
- auto const& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
+ auto const& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only, selected_suite);
if (do_list_cases) {
outln("Available cases for {}:", suite_name);
for (auto const& test : matching_tests) {
- outln(" {}", test->name());
+ outln(" {}::{}", LexicalPath(test->source()).title(), test->name());
}
return 0;
}
+ Vector<ByteString> sources;
+ for (auto const& test : matching_tests) {
+ if (!sources.contains_slow(test->source()))
+ sources.append(test->source());
+ }
+ if (selected_suite.is_empty() && sources.size() > 1) {
+ auto executable = Core::System::current_executable_path();
+ if (executable.is_error()) {
+ warnln("Cannot find the test executable: {}", executable.error());
+ return 1;
+ }
+ int failures = 0;
+ for (auto const& source : sources) {
+ Vector<ByteString> child_arguments;
+ for (auto argument : arguments.slice(1))
+ child_arguments.append(ByteString { argument });
+ child_arguments.append("--suite");
+ child_arguments.append(LexicalPath(source).title());
+ warnln("Running suite '{}'.", LexicalPath(source).title());
+ auto process = Core::Process::spawn(executable.value(), child_arguments);
+ if (process.is_error()) {
+ warnln("Cannot start the suite: {}", process.error());
+ ++failures;
+ continue;
+ }
+ auto status = process.value().wait_for_termination();
+ if (status.is_error() || status.value() != 0)
+ ++failures;
+ }
+ return failures;
+ }
+ if (!selected_suite.is_empty() && matching_tests.is_empty()) {
+ warnln("No cases match suite '{}' and pattern '{}'.", selected_suite, search_string);
+ return 1;
+ }
+ if (!sources.is_empty()) {
+ if (auto setup = m_setups.find(sources.first()); setup != m_setups.end())
+ setup->value();
+ }
+
outln("Running {} cases out of {}.", matching_tests.size(), m_cases.size());
return run(matching_tests);
}
-Vector<NonnullRefPtr<TestCase>> TestSuite::find_cases(ByteString const& search, bool find_tests, bool find_benchmarks)
+Vector<NonnullRefPtr<TestCase>> TestSuite::find_cases(ByteString const& search, bool find_tests, bool find_benchmarks, StringView suite)
{
Vector<NonnullRefPtr<TestCase>> matches;
for (auto& t : m_cases) {
- if (!search.is_empty() && !t->name().matches(search, CaseSensitivity::CaseInsensitive)) {
+ auto source_path = LexicalPath(t->source());
+ if (!suite.is_empty() && source_path.title() != suite)
+ continue;
+ auto qualified_name = ByteString::formatted("{}::{}", source_path.title(), t->name());
+ if (!search.is_empty() && !t->name().matches(search, CaseSensitivity::CaseInsensitive) && !qualified_name.matches(search, CaseSensitivity::CaseInsensitive)) {
continue;
}
diff --git a/Libraries/LibTest/TestSuite.h b/Libraries/LibTest/TestSuite.h
index 2f036bd45fc..e46f9b3b145 100644
--- a/Libraries/LibTest/TestSuite.h
+++ b/Libraries/LibTest/TestSuite.h
@@ -9,6 +9,7 @@
#include <AK/ByteString.h>
#include <AK/Function.h>
+#include <AK/HashMap.h>
#include <AK/Time.h>
#include <AK/Vector.h>
#include <AK/kmalloc.h>
@@ -40,7 +41,7 @@ public:
int run(Vector<NonnullRefPtr<TestCase>> const&);
int main(ByteString const& suite_name, Span<StringView> arguments);
- Vector<NonnullRefPtr<TestCase>> find_cases(ByteString const& search, bool find_tests, bool find_benchmarks);
+ Vector<NonnullRefPtr<TestCase>> find_cases(ByteString const& search, bool find_tests, bool find_benchmarks, StringView suite = {});
void add_case(NonnullRefPtr<TestCase> const& test_case)
{
m_cases.append(test_case);
@@ -49,7 +50,7 @@ public:
TestResult current_test_result() const { return m_current_test_result; }
void set_current_test_result(TestResult result) { m_current_test_result = result; }
- void set_suite_setup(Function<void()> setup) { m_setup = move(setup); }
+ void set_suite_setup(ByteString const& source, Function<void()> setup) { m_setups.set(source, move(setup)); }
// The RandomnessSource is where generators record / replay random data
// from. Initially a live "truly random" RandomnessSource is used, and when
// a failure is found, a set of hardcoded RandomnessSources is used during
@@ -75,7 +76,7 @@ private:
ByteString m_suite_name;
u64 m_benchmark_repetitions = 1;
u64 m_randomized_runs = 100;
- Function<void()> m_setup;
+ HashMap<ByteString, Function<void()>> m_setups;
TestResult m_current_test_result = TestResult::NotRun;
Randomized::RandomnessSource m_randomness_source = Randomized::RandomnessSource::live();
bool m_reporting_enabled = true;
diff --git a/Meta/CMake/targets.cmake b/Meta/CMake/targets.cmake
index 2472f71fe71..282a2f8d7df 100644
--- a/Meta/CMake/targets.cmake
+++ b/Meta/CMake/targets.cmake
@@ -205,6 +205,12 @@ function(ladybird_test test_src sub_dir)
endif()
endfunction()
+# Each library's sources are added directly to its test executable.
+function(ladybird_test_suite sub_dir)
+ string(REPLACE "/" "" suite_name "Test${sub_dir}")
+ ladybird_test("" "${sub_dir}" NAME "${suite_name}" ${ARGN})
+endfunction()
+
function(ladybird_utility name)
cmake_parse_arguments(LADYBIRD_UTILITY "" "" "SOURCES;LIBS" ${ARGN})
diff --git a/Tests/LibTest/CMakeLists.txt b/Tests/LibTest/CMakeLists.txt
index 19a42d1af05..b473edf06cc 100644
--- a/Tests/LibTest/CMakeLists.txt
+++ b/Tests/LibTest/CMakeLists.txt
@@ -1,8 +1,10 @@
+ladybird_test_suite(LibTest)
+
set(TEST_SOURCES
TestNoCrash.cpp
TestGenerator.cpp
+ TestSuiteIsolation.cpp
+ TestSuiteIsolationPeer.cpp
)
-foreach(source IN LISTS TEST_SOURCES)
- ladybird_test("${source}" Test)
-endforeach()
+target_sources(TestLibTest PRIVATE ${TEST_SOURCES})
diff --git a/Tests/LibTest/TestSuiteIsolation.cpp b/Tests/LibTest/TestSuiteIsolation.cpp
new file mode 100644
index 00000000000..3ed0fd258dd
--- /dev/null
+++ b/Tests/LibTest/TestSuiteIsolation.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCore/Environment.h>
+#include <LibTest/TestCase.h>
+#include <LibTest/TestSuite.h>
+
+static int setup_value;
+
+TEST_SETUP
+{
+ setup_value = 1;
+}
+
+TEST_CASE(setup_and_process_state_are_isolated)
+{
+ EXPECT_EQ(setup_value, 1);
+ EXPECT(!Core::Environment::has("LADYBIRD_TEST_SUITE_STATE"sv));
+ MUST(Core::Environment::set("LADYBIRD_TEST_SUITE_STATE"sv, "changed"sv, Core::Environment::Overwrite::Yes));
+}
+
+TEST_CASE(suites_are_selected_by_name)
+{
+ Test::TestSuite suite;
+ auto test_case = make_ref_counted<Test::TestCase>("example"sv, "/Tests/TestExample.cpp", [] { }, false);
+ suite.add_case(test_case);
+ suite.add_case(make_ref_counted<Test::TestCase>("example"sv, "/Tests/TestExamplePeer.cpp", [] { }, false));
+
+ auto matches = suite.find_cases("*", true, false, "TestExample"sv);
+ EXPECT_EQ(matches.size(), 1u);
+ if (matches.size() == 1)
+ EXPECT_EQ(matches.first().ptr(), test_case.ptr());
+
+ EXPECT(suite.find_cases("*", true, false, "TestExample.cpp"sv).is_empty());
+ EXPECT(suite.find_cases("*", true, false, "/Tests/TestExample.cpp"sv).is_empty());
+ EXPECT(suite.find_cases("*", true, false, "TestExam"sv).is_empty());
+ EXPECT_EQ(suite.find_cases("TestExample::*", true, false).size(), 1u);
+ EXPECT(suite.find_cases("TestExample.cpp::*", true, false).is_empty());
+}
diff --git a/Tests/LibTest/TestSuiteIsolationPeer.cpp b/Tests/LibTest/TestSuiteIsolationPeer.cpp
new file mode 100644
index 00000000000..83707b68e99
--- /dev/null
+++ b/Tests/LibTest/TestSuiteIsolationPeer.cpp
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCore/Environment.h>
+#include <LibTest/TestCase.h>
+
+static int setup_value;
+
+TEST_SETUP
+{
+ setup_value = 2;
+}
+
+TEST_CASE(setup_and_process_state_are_isolated)
+{
+ EXPECT_EQ(setup_value, 2);
+ EXPECT(!Core::Environment::has("LADYBIRD_TEST_SUITE_STATE"sv));
+ MUST(Core::Environment::set("LADYBIRD_TEST_SUITE_STATE"sv, "changed"sv, Core::Environment::Overwrite::Yes));
+}