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
Here is an example structure for testing with Sus - the actual structure may vary based on your gem's organization, but aside from the `lib/` directory, sus expects the following structure:
# Use the covered gem for test coverage reporting:
80
+
require"covered/sus"
81
+
includeCovered::Sus
82
+
83
+
defbefore_tests(assertions, output:self.output)
84
+
# Starts the clock and sets up the test environment:
85
+
super
86
+
end
87
+
88
+
defafter_tests(assertions, output:self.output)
89
+
# Stops the clock and prints the test results:
90
+
super
91
+
end
92
+
```
93
+
94
+
### Fixtures Files
95
+
96
+
`fixtures/` gets added to the `$LOAD_PATH` automatically, so you can require files from there without needing to specify the full path.
97
+
98
+
### Test Files
99
+
100
+
Sus runs all Ruby files in the `test/` directory by default. But you can also create tests in any file, and run them with the `sus my_tests.rb` command.
101
+
102
+
## Test Syntax
103
+
104
+
### `describe` - Test Groups
105
+
106
+
Use `describe` to group related tests:
107
+
108
+
```ruby
109
+
describe MyThingdo
110
+
# The subject will be whatever is described:
111
+
let(:my_thing) {subject.new}
112
+
end
113
+
```
114
+
115
+
### `it` - Individual Tests
116
+
117
+
Use `it` to define individual test cases:
118
+
119
+
```ruby
120
+
it "returns the expected value"do
121
+
expect(result).to be =="expected"
122
+
end
123
+
```
124
+
125
+
You can use `it` blocks at the top level or within `describe` or `with` blocks.
126
+
127
+
### `with` - Context Blocks
128
+
129
+
Use `with` to create context-specific test groups:
You can create custom predicates for more complex assertions:
320
+
321
+
```ruby
322
+
defbe_small_prime
323
+
(be ==2).or(be ==3, be ==5, be ==7)
324
+
end
325
+
```
326
+
327
+
## Block Expectations
328
+
329
+
### Testing Blocks
330
+
331
+
```ruby
332
+
expect{operation}.to raise_exception(Error)
333
+
expect{operation}.to have_duration(be <1.0)
334
+
```
335
+
336
+
### Performance Testing
337
+
338
+
You should generally avoid testing performance in unit tests, as it will be highly unstable and dependent on the environment. However, if you need to test performance, you can use:
339
+
340
+
```ruby
341
+
expect{slow_operation}.to have_duration(be <2.0)
342
+
expect{fast_operation}.to have_duration(be <0.1)
343
+
```
344
+
345
+
- For less unstable performance tests, you can use the `sus-fixtures-time` gem which tries to compensate for the environment by measuring execution time.
346
+
347
+
- For benchmarking, you can use the `sus-fixtures-benchmark` gem which measures a block of code multiple times and reports the execution time.
348
+
349
+
## File Operations
350
+
351
+
### Temporary Directories
352
+
353
+
Use `Dir.mktmpdir` for isolated test environments:
354
+
355
+
```ruby
356
+
around do |block|
357
+
Dir.mktmpdir do |root|
358
+
@root= root
359
+
block.call
360
+
end
361
+
end
362
+
363
+
let(:test_path) {File.join(@root, "test.txt")}
364
+
365
+
it "can create a file"do
366
+
File.write(test_path, "content")
367
+
expect(File).to be(:exist?, test_path)
368
+
end
369
+
```
370
+
371
+
## Test Output
372
+
373
+
In general, tests should not produce output unless there is an error or failure.
374
+
375
+
### Informational Output
376
+
377
+
You can use `inform` to print informational messages during tests:
378
+
379
+
```ruby
380
+
it "logs an informational message"do
381
+
rate = copy_data(source, destination)
382
+
inform "Copied data at #{rate}MB/s"
383
+
expect(rate).to be >0
384
+
end
385
+
```
386
+
387
+
This can be useful for debugging or providing context during test runs.
388
+
389
+
### Console Output
390
+
391
+
The `sus-fixtures-console` gem provides a way to suppress and capture console output during tests. If you are using code which generates console output, you can use this gem to capture it and assert on it.
0 commit comments