liveBook · Manning

testList

As explained in F# in Action: A Revised Edition of Get Programming with F#

testList is an Expecto function that constructs a composite test from a name and a collection of child tests.

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

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains grouping, nesting, and programmatic construction, while stopping short of framework-specific discovery mechanisms.
Examples
Demonstrates music tests, nested database and business-logic groups, CI-conditional tests, and additions generated from a data file.
What it assumes
Assumes basic F# test functions and assertions, while using ordinary lists, conditionals, loops, and file input.
What you can do after
Apply it via Expecto to compose, name, and execute structured or data-driven test suites.
How it connects
Connects testList with Test, test, Expecto, runTestsWithCLIArgs, test runners, NUnit, and XUnit.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.