testList constructs a named composite Expecto test from a collection of child tests. Because the result is itself a Test, groups can be nested, passed around, and run as one hierarchy. section 14.2.3 section 14.2.4
Use testList when tests need a clear structure rather than framework-managed discovery:
- Group individual test calls under a readable name such as "Music tests". section 14.2.3
- Nest database, business-logic, and larger aggregate groups into an "All tests" hierarchy. section 14.2.4
- Build children programmatically with ordinary F# conditionals, loops, environment variables, and input files. section 14.2.4
A small grouped test suite
let musicTests = testList "Music tests" [ test "My guitar gently weeps" { Expect.equal (play Guitar) "Wah wah" "Guitar sound is incorrect" } test "Drums go bang" { Expect.equal (play Drums) "Ba dum tss" "Drums are wrong" } ] [<EntryPoint>] let main args = runTestsWithCLIArgs [] args musicTests The group name becomes part of each test's fully qualified output, making failures easier to locate. section 14.2.3
Listing 14.4 Programmatically creating tests in Expecto
let add a b = a + b #1 let myTests = testList "My Tests" [ if System.Environment.GetEnvironmentVariable "CI_SERVER" <> null then #2 test "CI Only Test" { Expect.equal (add 1 2) 3 "Something is very wrong!" } for line in System.IO.File.ReadAllLines "testData.txt" do #3 test $"Addition test: {line}" { #4 match line.Split ',' with | [| inputA; inputB; expected |] -> let actual = add (int inputA) (int inputB) Expect.equal actual (int expected) "Calculator is broken" | _ -> failwith $"Invalid test data format: {line}" } ] #1 Our production code, a simple addition function #2 Programmatically decides whether to yield back a test based on the existence of an environment variable #3 Reads data from a file using basic .NET code #4 For each line in the file, yields back a new test whose name is based on the line in the file
Inspect a data-driven suite that conditionally adds a CI test and generates addition tests from testData.txt. section 14.2.4
