感觉在单元测试当中可能有用,
但在django这种框架中,用途另一说。
import pytestimport tasksfrom tasks import Taskdef test_add_1(): task = Task('breathe', 'Brian', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)@pytest.mark.parametrize('task', [Task('sleep', done=True), Task('wake', 'brian'), Task('breath', 'BRIAN', True), Task('exercise', 'BrIaN', False)])def test_add_2(task): task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)@pytest.mark.parametrize('summary, owner, done', [('sleep', None, False), ('wake', 'brian', False), ('breath', 'BRIAN', True), ('eat eggs', 'BrIaN', False), ])def test_add_3(summary, owner, done): task = Task(summary, owner, done) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)tasks_to_try = (Task('sleep', done=True), Task('wake', 'brian'), Task('breath', 'BRIAN', True), Task('exercise', 'BrIaN', False))@pytest.mark.parametrize('task', tasks_to_try)def test_add_4(task): task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)task_ids = ['Task({},{},{})'.format(t.summary, t.owner, t.done) for t in tasks_to_try]@pytest.mark.parametrize('task', tasks_to_try, ids=task_ids)def test_add_5(task): task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) @pytest.mark.parametrize('task', [ pytest.param(Task('create'), id='just summary'), pytest.param(Task('inspire', 'Michelle'), id='summary/owner'), pytest.param(Task('encourage', 'Michelle', True), id='summary/owner/done'),])def test_add_6(task): task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)@pytest.mark.parametrize('task', tasks_to_try, ids=task_ids)class TestAdd: def test_equivalent(self, task): task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) def test_valid_id(self, task): task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert t_from_db.id == task_iddef equivalent(t1, t2): return (t1.summary == t2.summary) and (t1.owner == t2.owner) and (t1.done == t2.done)@pytest.fixture(autouse=True)def initialized_tasks_db(tmpdir): tasks.start_tasks_db(str(tmpdir), 'tiny') yield tasks.stop_tasks_db()