User Case
Imagine you want to write a test for a particular function, but for multiple input values. Writing a for-loop is a bad idea as the test will fail as soon as it hits the first AssertionError. Subsequent input values will not be tested and you have no idea which part of your code is actually broken. At the same time you want to stick to DRY and not implement the same unittest.Testcase method over and over again with slightly different input values.
Keep in mind why we write unit tests:
Pytest provides various ways of creating individual test items. There are parametrized fixtures and mark.parametrize (and hooks).
Marker
Using this built-in marker @pytest.mark.parametrize you do not need to implement any fixtures. Instead you define your scenarios in a decorator and the only thing you really need to look out for is to match the number of positional test arguments with your iterable.
- test1.py
- import pytest
- @pytest.mark.parametrize(
- 'number, word', [
- (1, '1'),
- (3, 'Fizz'),
- (5, 'Buzz'),
- (10, 'Buzz'),
- (15, 'FizzBuzz'),
- (16, '16')
- ]
- )
- def test_fizzbuzz(number, word):
- assert fizzbuzz(number) == word
To parametrize a fixture you need pass an iterable to the params keyword argument. The built-in fixture request knows about the current parameter and if you don't want to do anything fancy, you can pass it right to the test via the returnstatement.
- test2.py
- import pytest
- @pytest.fixture(params=[('1+1', 2), ('1*3', 3), ('1+2*3', 7)])
- def calc(request):
- return request.param
- def test_calc(calc):
- print("{}".format(str(calc)))
- assert eval(calc[0]) == calc[1]
Example Implementation
Sometimes you may find yourself struggling to chose which is the best way to parametrize your tests. At the end of the day it really depends on what you want to test. But... Good news! Pytest lets you combine both methods to get the most out of both worlds.
Some Classes in a Module
Imagine this Python module (foobar.py) which contains a few class definitions with a bit of logic:
- # -*- coding: utf-8 -*-
- FOSS_LICENSES = ['Apache 2.0', 'MIT', 'GPL', 'BSD']
- PYTHON_PKGS = ['pytest', 'requests', 'django', 'cookiecutter']
- class Package:
- def __init__(self, name, license):
- self.name = name
- self.license = license
- @property
- def is_open_source(self):
- return self.license in FOSS_LICENSES
- class Person:
- def __init__(self, name, gender):
- self.name = name
- self.gender = gender
- self._skills = ['eating', 'sleeping']
- def learn(self, skill):
- self._skills.append(skill)
- @property
- def looks_like_a_programmer(self):
- return any(
- package in self._skills
- for package in PYTHON_PKGS
- )
- class Woman(Person):
- def __init__(self, name):
- super().__init__(name, 'female')
- class Man(Person):
- def __init__(self, name):
- super().__init__(name, 'male')
With only two few lines of pytest code, we can create loads of different scenarios that we would like to test. By re-using parametrized fixtures and applying the aforementioned markers to your tests, you can focus on the actual test implementation, as opposed to writing the same boilerplate code for each of the methods that you would have to write with unittest.Testcase.
- test3.py
- # -*- coding: utf-8 -*-
- import operator
- import pytest
- from foobar import Package, Woman, Man
- PACKAGES = [
- Package('requests', 'Apache 2.0'),
- Package('django', 'BSD'),
- Package('pytest', 'MIT'),
- ]
- @pytest.fixture(params=PACKAGES, ids=operator.attrgetter('name'))
- def python_package(request):
- return request.param
- @pytest.mark.parametrize('person', [
- Woman('Audrey'), Woman('Brianna'),
- Man('Daniel'), Woman('Ola'), Man('Kenneth')
- ])
- def test_become_a_programmer(person, python_package):
- person.learn(python_package.name)
- assert person.looks_like_a_programmer
- def test_is_open_source(python_package):
- assert python_package.is_open_source
Going the extra mile and setting up ids for your test scenarios greatly increases the comprehensibilty of your test report. In this case we would like to display the name of each Package rather than the fixture name with a numbered suffix such as python_package2.
沒有留言:
張貼留言