Pytest – is there a way to not have to re write a test function three times?
Question:
Is there a way to not have to re write this test function three times? Maybe a fixture
so I can use a param / arg?
Tank you ever so much for reading.
from kingarthur import data
from pathlib import Path
def test_BuildZoneA():
zone_path = Path('home/stevethewizard/zoneA.txt')
data.BuildZone('t', 's', zone_path, source_list=['spell1', 'spell2'])
assert zone_path.is_file() == True
assert zone_path.stat().st_size > 0
def test_BuildZoneB():
zone_path = Path('home/stevethewizard/zoneB.txt')
data.BuildZone('t', 's', zone_path, cust_source_path='home/stevethewizard/spells/spells_master.txt')
assert zone_path.is_file() == True
assert zone_path.stat().st_size > 0
def test_BuildZoneC():
zone_path = Path('home/stevethewizard/zoneC.txt')
data.BuildZone('t', 's', zone_path)
assert zone_path.is_file() == True
assert zone_path.stat().st_size > 0
Answers:
If you’re using pytest, you can parametrize your tests using pytest.mark.parametrize
. For example:
import pytest
from kingarthur import data
from pathlib import Path
@pytest.mark.parametrize("zone_path,source_list,source_path", [
(Path("/home/stevethewizard/ZoneA.txt"), ['spell1', 'spell2'], None),
(Path("/home/stevethewizard/ZoneB.txt"), None, ".../spells_master.txt"),
(Path("/home/stevethewizard/ZoneC.txt"), None, None),
])
def test_BuildZone(zone_path, source_list, source_path):
data.BuildZone('t', 's', zone_path, source_list=source_list, cust_source_path=source_path)
assert zone_path.is_file() == True
assert zone_path.stat().st_size > 0
Read the linked documentation for more information.
Is there a way to not have to re write this test function three times? Maybe a fixture
so I can use a param / arg?
Tank you ever so much for reading.
from kingarthur import data
from pathlib import Path
def test_BuildZoneA():
zone_path = Path('home/stevethewizard/zoneA.txt')
data.BuildZone('t', 's', zone_path, source_list=['spell1', 'spell2'])
assert zone_path.is_file() == True
assert zone_path.stat().st_size > 0
def test_BuildZoneB():
zone_path = Path('home/stevethewizard/zoneB.txt')
data.BuildZone('t', 's', zone_path, cust_source_path='home/stevethewizard/spells/spells_master.txt')
assert zone_path.is_file() == True
assert zone_path.stat().st_size > 0
def test_BuildZoneC():
zone_path = Path('home/stevethewizard/zoneC.txt')
data.BuildZone('t', 's', zone_path)
assert zone_path.is_file() == True
assert zone_path.stat().st_size > 0
If you’re using pytest, you can parametrize your tests using pytest.mark.parametrize
. For example:
import pytest
from kingarthur import data
from pathlib import Path
@pytest.mark.parametrize("zone_path,source_list,source_path", [
(Path("/home/stevethewizard/ZoneA.txt"), ['spell1', 'spell2'], None),
(Path("/home/stevethewizard/ZoneB.txt"), None, ".../spells_master.txt"),
(Path("/home/stevethewizard/ZoneC.txt"), None, None),
])
def test_BuildZone(zone_path, source_list, source_path):
data.BuildZone('t', 's', zone_path, source_list=source_list, cust_source_path=source_path)
assert zone_path.is_file() == True
assert zone_path.stat().st_size > 0
Read the linked documentation for more information.