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 57 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.
Next: Gems against pip
Article author: Arthur Isaev