0
# Test Suites
1
2
Core test suite classes that provide the foundation for writing and organizing tests in MUnit. Users extend these classes to create test suites with different levels of functionality and customization.
3
4
## Capabilities
5
6
### FunSuite - Function-Style Test Suite
7
8
The primary test suite class that most users should extend. Provides a simple `test()` method for defining tests with automatic async support.
9
10
```scala { .api }
11
/**
12
* Main test suite class for function-style tests
13
*/
14
abstract class FunSuite extends BaseFunSuite
15
16
trait BaseFunSuite
17
extends Suite
18
with Assertions
19
with FunFixtures
20
with TestOptionsConversions
21
with TestTransforms
22
with SuiteTransforms
23
with ValueTransforms {
24
25
/** Define a test with a name and body */
26
def test(name: String)(body: => Any)(implicit loc: Location): Unit
27
28
/** Define a test with options and body */
29
def test(options: TestOptions)(body: => Any)(implicit loc: Location): Unit
30
31
/** Configure test timeout (default: 30 seconds) */
32
def munitTimeout: Duration
33
34
/** Get all tests in this suite */
35
def munitTests(): Seq[Test]
36
}
37
```
38
39
**Usage Examples:**
40
41
```scala
42
import munit.FunSuite
43
import scala.concurrent.Future
44
45
class MyTestSuite extends FunSuite {
46
test("simple test") {
47
assertEquals(2 + 2, 4)
48
}
49
50
test("test with custom options".tag(Slow)) {
51
// This test is marked as slow
52
Thread.sleep(1000)
53
assert(true)
54
}
55
56
test("async test") {
57
Future.successful(42).map { result =>
58
assertEquals(result, 42)
59
}
60
}
61
62
test("test that should fail".fail) {
63
// This test is expected to fail
64
assertEquals(1, 2)
65
}
66
}
67
```
68
69
### Suite - Base Test Suite
70
71
Minimal base class for custom test suites. Use this when you need full control over test execution and don't need the convenience methods provided by `FunSuite`.
72
73
```scala { .api }
74
/**
75
* Base class for all test suites with minimal functionality
76
*/
77
@RunWith(classOf[MUnitRunner])
78
abstract class Suite extends PlatformSuite {
79
80
// Type aliases for convenience
81
final type TestValue = Future[Any]
82
final type Fixture[T] = munit.Fixture[T]
83
final type Test = munit.Test
84
final type BeforeEach = munit.BeforeEach
85
final type AfterEach = munit.AfterEach
86
87
/** Abstract method to return all tests in the suite */
88
def munitTests(): Seq[Test]
89
90
/** Suite-level fixtures for resource management */
91
def munitFixtures: Seq[AnyFixture[_]] = Nil
92
93
/** Execution context for async operations */
94
def munitExecutionContext: ExecutionContext
95
96
/** Runs once before all tests in the suite */
97
def beforeAll(): Unit = ()
98
99
/** Runs once after all tests in the suite */
100
def afterAll(): Unit = ()
101
102
/** Runs before each individual test */
103
def beforeEach(context: BeforeEach): Unit = ()
104
105
/** Runs after each individual test */
106
def afterEach(context: AfterEach): Unit = ()
107
}
108
```
109
110
**Usage Examples:**
111
112
```scala
113
import munit._
114
import scala.concurrent.Future
115
116
class CustomSuite extends Suite {
117
private val tests = List(
118
new Test("custom test 1", () => Future.successful(assert(true)), Set.empty, Location.empty),
119
new Test("custom test 2", () => Future.successful(assertEquals(1 + 1, 2)), Set.empty, Location.empty)
120
)
121
122
def munitTests(): Seq[Test] = tests
123
124
override def beforeAll(): Unit = {
125
println("Setting up test suite")
126
}
127
128
override def afterAll(): Unit = {
129
println("Tearing down test suite")
130
}
131
132
override def beforeEach(context: BeforeEach): Unit = {
133
println(s"Running test: ${context.test.name}")
134
}
135
}
136
```
137
138
### Platform-Specific Components
139
140
Platform-specific base traits that provide platform-dependent functionality.
141
142
```scala { .api }
143
/**
144
* Platform-specific base functionality
145
* Implementation varies by platform (JVM/JS/Native)
146
*/
147
trait PlatformSuite
148
```
149
150
**Platform-Specific Features:**
151
152
For JavaScript and Native platforms:
153
154
```scala { .api }
155
/**
156
* Annotation to ignore an entire test suite (JS/Native only)
157
*/
158
class IgnoreSuite extends Annotation
159
```
160
161
## Suite Configuration
162
163
### Lifecycle Hooks
164
165
All suite classes provide hooks for setup and teardown operations:
166
167
- `beforeAll()`: Runs once before all tests
168
- `afterAll()`: Runs once after all tests
169
- `beforeEach(context)`: Runs before each test
170
- `afterEach(context)`: Runs after each test
171
172
### Execution Context
173
174
Suites can customize the execution context for async operations by overriding `munitExecutionContext`. The default is a parasitic execution context that runs tasks synchronously.
175
176
### Test Collection
177
178
The `munitTests()` method returns all tests for the suite. In `FunSuite`, this is handled automatically, but custom suites need to implement this method.