Minitest against pytest
One class, two tests
A test is a test in both languages: call a method, compare the result, report one line. The class under test is a single method that adds two numbers, written once in Ruby and once in Python.
class Calc def add(a, b) a + b end end
def add(a, b): return a + b
Minitest does not need a file name convention: the test requires the library and names a class.
require "minitest/autorun" require_relative "../lib/calc" class CalcTest < Minitest::Test def test_add assert_equal 4, Calc.new.add(2, 2) end end
pytest finds the test by its file name and its function name; no class and no require line are needed.
from calc import add def test_add(): assert add(2, 2) == 4
Run both
ruby -Itest test/calc_test.rb
Run options: --seed 44186
# Running:
.
Finished in 0.000722s, 1385.2891 runs/s, 1385.2891 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
python3 -m pytest -q
.
1 passed in 0.01s
Minitest prints a run header, a dot per test and a summary line. pytest with -q prints a dot and one line. Both exit zero, which is the only part a pipeline reads.
What differs
- Discovery. Minitest runs the file you name (or the files a Rake task names); pytest walks the folder for test_*.py files by itself.
- Assertions. assert_equal expected, actual carries the expected value in the call; Python's plain assert inspects the expression, so the report shows both sides without a special method.
- Classes. Minitest groups tests in a class by default; pytest uses plain functions and adds classes only when a test needs shared state.
- Fixtures. Both have setup hooks; pytest's @pytest.fixture also injects values into a test signature, which Minitest does with a method call in setup.
What to watch for
A pipeline reads the exit code and the report file, not the pretty
output. Lesson 13.7 turns Minitest into JUnit XML;
pytest --junitxml=report.xml does the same on the
Python side, so a mixed repository can feed both into one report view.
Run the tests through the project task (rake test,
pytest), not through a hand-written file list.
The wrapper is where the seed, the report and the coverage flags go, and
both pipelines then call one command.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How does each framework find its tests?
Answer
Minitest runs the file you name, pytest walks the folder for test_*.py files
A Rake task usually names the Minitest files, while pytest discovers the Python tests by their file and function names.
-
What does this program print?
def add(a, b) = a + b raise "wrong" unless add(2, 2) == 4 puts "ok"Answer
ok
The condition holds, so the guard does not fire and the program reports success.
-
What does this program print?
def add(a, b) = a + b begin raise "expected 5, got #{add(2, 2)}" unless add(2, 2) == 5 rescue RuntimeError => e puts e.message endAnswer
expected 5, got 4
The message puts the expected value first, which is the order assert_equal uses in Minitest.
-
What does this program print?
files = ["test_calc.py", "calc.py", "tests/test_math.py"] p files.select { |f| f.start_with?("test_") }Answer
["test_calc.py"]
The prefix rule keeps the file at the top level, and the path with a folder does not match it.
-
In
assert_equal expected, actual, which value comes first?Answer
The expected value
The expected value comes first, so a failure report says what was wanted and what the method returned.
-
What does this program print?
tests = [true, true, true] p tests.count { |t| t }Answer
3
Every element is true, which is the summary line a passing run shows.
-
What does a pytest fixture add over a plain setup method?
Answer
It injects a value into the test signature, which Minitest does with a call in setup
The dependency injection is the extra shape, and a Minitest class gets the same value from an instance variable set in setup.
-
Which flag writes a JUnit report from pytest?
Answer
--junitxml=report.xml
The flag is the pytest spelling of the report that lesson 13.7 builds for Minitest, so one report view can read both.
-
What does this program print?
p "test_add".start_with?("test_")Answer
true
The function name follows the discovery rule, which is why pytest finds it without a class or a list.
-
What does a pipeline read from a test run?
Answer
The exit code and the report file, not the pretty output
A green run is a zero status, and the report is the artifact that survives the log.
Article author: Arthur Isaev