Minitest basics (assert style) in Ruby - Time & Space Complexity
When we write tests using Minitest's assert style, it's important to know how long these tests take as the code grows.
We want to understand how the time to run tests changes when we add more checks or bigger inputs.
Analyze the time complexity of the following Minitest assert style test.
require 'minitest/autorun'
class TestSum < Minitest::Test
def test_sum
n = 1000
numbers = (1..n).to_a
sum = numbers.reduce(0, :+)
assert_equal n * (n + 1) / 2, sum
end
end
This test checks if the sum of numbers from 1 to n is correct using the reduce method and an assertion.
Look at what repeats when the test runs.
- Primary operation: Summing numbers from 1 to n using reduce.
- How many times: The sum operation goes through all n numbers once.
As n grows, the sum operation takes longer because it adds more numbers.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The number of operations grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the test takes longer in a straight line as the input size n grows.
[X] Wrong: "The assertion itself takes a lot of time as n grows."
[OK] Correct: The assertion just compares two numbers, which is very fast and does not depend on n.
Understanding how test time grows helps you write efficient tests and shows you care about quality and speed in real projects.
"What if we replaced the reduce method with a direct formula calculation? How would the time complexity change?"
Practice
assert method in Minitest do?Solution
Step 1: Understand the purpose of
Theassertassertmethod is used to verify that a condition is true during a test.Step 2: What happens if the condition is false?
If the condition is false, the test fails and reports an error.Final Answer:
Checks if a condition is true and fails the test if not -> Option AQuick Check:
assert checks truth = B [OK]
- Thinking assert runs all tests
- Confusing assert with printing output
- Believing assert defines test classes
Solution
Step 1: Identify Ruby method syntax for Minitest
In Minitest, test methods start withdef test_...and end withend.Step 2: Check the assert usage inside the method
assert_equal expected, actualis used correctly inside the method.Final Answer:
def test_example; assert_equal 5, 2 + 3; end -> Option BQuick Check:
Ruby test method syntax = C [OK]
- Using non-Ruby syntax like function or braces
- Using test do blocks which belong to other frameworks
- Missing def or end keywords
require 'minitest/autorun'
class SampleTest < Minitest::Test
def test_sum
assert_equal 10, 5 + 5
end
def test_fail
assert 3 > 5, '3 is not greater than 5'
end
endSolution
Step 1: Analyze
test_summethodassert_equal 10, 5 + 5checks if 10 equals 10, which is true, so this test passes.Step 2: Analyze
test_failmethodassert 3 > 5checks if 3 is greater than 5, which is false, so this test fails and shows the message '3 is not greater than 5'.Final Answer:
One test passes, one test fails with message '3 is not greater than 5' -> Option AQuick Check:
assert_equal true, assert false with message = D [OK]
- Assuming both tests pass
- Confusing assert and assert_equal behavior
- Ignoring failure message in assert
class MyTest < Minitest::Test
def test_check
assert_equal(4, 2 + 2
end
endSolution
Step 1: Check syntax of assert_equal call
The lineassert_equal(4, 2 + 2is missing a closing parenthesis.Step 2: Verify other parts of the code
Class inheritance and method name are correct, and argument order is correct (expected, actual).Final Answer:
Missing closing parenthesis in assert_equal call -> Option CQuick Check:
Syntax errors often from missing parentheses = A [OK]
- Ignoring missing parentheses causing syntax errors
- Thinking test method name can be arbitrary
- Confusing argument order in assert_equal
greet(name) returns 'Hello, name!'. Which assert statement correctly tests this behavior?Solution
Step 1: Understand assert_equal argument order
The first argument is the expected value, the second is the actual result from the method.Step 2: Check each option's argument order and syntax
assert_equal 'Hello, Alice!', greet('Alice')correctly uses expected first, actual second.assert 'Hello, Alice!', greet('Alice')asserts a truthy string (always passes), ignoring actual result.assert_equal greet('Alice'), 'Hello Alice!'reverses order and uses wrong expected (missing comma).assert greet('Alice') = 'Hello, Alice!'uses assignment (=), syntax error.Final Answer:
assert_equal 'Hello, Alice!', greet('Alice') -> Option DQuick Check:
Expected first, actual second in assert_equal = A [OK]
- Swapping expected and actual arguments
- Using assignment (=) instead of comparison (==)
- Using assert with assignment causing syntax error
Ready to prove it?
Pass the test and this pattern moves unknown
