You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inline tests let you write test cases directly on the method being tested using the `#[TestInline]` attribute. No separate test class needed.
4
+
5
+
```php
6
+
use Testo\Sample\TestInline;
7
+
8
+
#[TestInline([1, 1], 2)]
9
+
#[TestInline([40, 2], 42)]
10
+
#[TestInline([-5, 5], 0)]
11
+
public function sum(int $a, int $b): int
12
+
{
13
+
return $a + $b;
14
+
}
15
+
```
16
+
17
+
Each attribute runs the method with the given arguments and verifies the result.
18
+
19
+
## When to Use
20
+
21
+
Inline tests work well for:
22
+
-**Simple pure functions** where a dedicated test file would be excessive
23
+
-**Private helper methods** that you want to test without changing visibility
24
+
-**Prototyping** when you need immediate validation without switching context
25
+
26
+
For larger test suites (10+ cases) or tests that need explanation, consider writing separate tests with [DataProvider](./sample-module).
27
+
28
+
## Configuration
29
+
30
+
It's recommended to create a separate Test Suite for inline tests. Since inline tests live in your application code (not in `tests/`), you don't need other test finders there — only `TestInlineFinder`.
Copy file name to clipboardExpand all lines: docs/sample-module.md
+2-101Lines changed: 2 additions & 101 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ The Sample module provides attributes for parameterized testing - running the sa
4
4
5
5
Currently includes:
6
6
-**DataProvider** - for dynamic, complex data sets
7
-
-**TestInline** - for simple, static test cases right on the method
7
+
-**[TestInline](./inline-tests)** - for simple, static test cases right on the method
8
8
9
9
## Data Provider
10
10
@@ -85,103 +85,4 @@ Use `DataProvider` when:
85
85
- Test cases need labels or descriptions for clarity
86
86
- You need complex setup logic for test data
87
87
88
-
**Note:**`DataProvider` is an addition to regular tests (methods marked with `#[Test]`). It provides data to existing test methods.
89
-
90
-
## Inline Tests
91
-
92
-
`TestInline` takes a different approach - it declares test cases as attributes directly on the method being tested, without requiring a separate test class.
93
-
94
-
This might be useful for simple pure functions where a dedicated test file would be excessive. It also works well for testing private helper methods - you can test them directly without changing visibility. When prototyping, `TestInline` gives you immediate validation without switching context to a test file.
Each `TestInline` attribute runs the method with the given arguments and verifies the result. Simple as that.
118
-
119
-
`TestInline` works best with 2-10 static test cases where the expected behavior is self-evident from the input/output pairs. For larger test suites or cases that need explanation, consider writing a separate test in the `tests/` directory using `DataProvider`.
120
-
121
-
### Testing Private Methods
122
-
123
-
This is where `TestInline` really shows its value. Need to test a private helper? Just add the attribute:
124
-
125
-
```php
126
-
#[TestInline(['password123'], false)] // too short
127
-
#[TestInline(['Password123!'], true)] // valid
128
-
#[TestInline(['pass'], false)] // no number
129
-
private function isStrongPassword(string $password): bool
130
-
{
131
-
return strlen($password) >= 8
132
-
&& preg_match('/[A-Z]/', $password)
133
-
&& preg_match('/[0-9]/', $password)
134
-
&& preg_match('/[^A-Za-z0-9]/', $password);
135
-
}
136
-
```
137
-
138
-
The method stays private - you don't need to expose it or write reflection code yourself. Testo handles that.
Встроенные тесты позволяют писать тесты прямо на тестируемом методе с помощью атрибута `#[TestInline]`. Отдельный тестовый класс не нужен.
4
+
5
+
```php
6
+
use Testo\Sample\TestInline;
7
+
8
+
#[TestInline([1, 1], 2)]
9
+
#[TestInline([40, 2], 42)]
10
+
#[TestInline([-5, 5], 0)]
11
+
public function sum(int $a, int $b): int
12
+
{
13
+
return $a + $b;
14
+
}
15
+
```
16
+
17
+
Каждый атрибут запускает метод с заданными аргументами и проверяет результат.
18
+
19
+
## Когда использовать
20
+
21
+
Встроенные тесты хорошо подходят для:
22
+
-**Простых чистых функций**, где отдельный тестовый файл был бы избыточным
23
+
-**Приватных вспомогательных методов**, которые хочется протестировать без изменения видимости
24
+
-**Прототипирования**, когда нужна быстрая проверка без переключения контекста
25
+
26
+
Для больших наборов тестов (10+ случаев) или тестов, требующих пояснений, лучше писать отдельные тесты с [DataProvider](./sample-module).
27
+
28
+
## Настройка
29
+
30
+
Рекомендуется создать отдельный Test Suite для inline тестов. Поскольку inline тесты находятся в коде приложения (не в `tests/`), прочие поисковики тестов там не нужны — только `TestInlineFinder`.
0 commit comments