Skip to content

Commit 67bccba

Browse files
committed
doc: add destructuring assignment examples to README
1 parent c6a748e commit 67bccba

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77

88
## [Unreleased]
9+
### Changed
10+
- DOC: add destructuring examples to README
911

1012

1113
## [2.0.0] — 2022-07-14

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fixture_name = lambda_fixture(lambda: asyncio.sleep(0, 'expression'), async_=Tru
4949
# Request fixtures by name
5050
fixture_name = lambda_fixture('other_fixture')
5151
fixture_name = lambda_fixture('other_fixture', 'another_fixture', 'cant_believe_its_not_fixture')
52+
ren, ame, it = lambda_fixture('other_fixture', 'another_fixture', 'cant_believe_its_not_fixture')
5253

5354
# Reference `self` inside a class
5455
class TestContext:
@@ -59,6 +60,8 @@ fixture_name = lambda_fixture(params=['a', 'b'])
5960
fixture_name = lambda_fixture(params=['a', 'b'], ids=['A!', 'B!'])
6061
fixture_name = lambda_fixture(params=[pytest.param('a', id='A!'),
6162
pytest.param('b', id='B!')])
63+
alpha, omega = lambda_fixture(params=[pytest.param('start', 'end', id='uno'),
64+
pytest.param('born', 'dead', id='dos')])
6265

6366
# Use literal value (not lazily evaluated)
6467
fixture_name = static_fixture(42)
@@ -125,6 +128,21 @@ def test_my_identity(who_i_am):
125128
assert who_i_am == ('Jason', 'Bourne')
126129
```
127130

131+
Destructuring assignment is also supported, allowing multiple fixtures to be renamed in one statement:
132+
```python
133+
# test_the_bourne_identity.py
134+
135+
from pytest_lambda import lambda_fixture, static_fixture
136+
137+
agent_first = static_fixture('Jason')
138+
agent_last = static_fixture('Bourne')
139+
first, last = lambda_fixture('agent_first', 'agent_last')
140+
141+
def test_my_identity(first, last):
142+
assert first == 'Jason'
143+
assert last == 'Bourne'
144+
```
145+
128146

129147
#### Annotating aliased fixtures
130148

@@ -147,6 +165,44 @@ def test_my_caddy(car):
147165
```
148166

149167

168+
### Parametrizing
169+
170+
Tests can be parametrized with `lambda_fixture`'s `params` kwarg
171+
```python
172+
# test_number_5.py
173+
174+
from pytest_lambda import lambda_fixture
175+
176+
lady = lambda_fixture(params=[
177+
'Monica', 'Erica', 'Rita', 'Tina', 'Sandra', 'Mary', 'Jessica'
178+
])
179+
180+
def test_your_man(lady):
181+
assert lady[:0] in 'my life'
182+
```
183+
184+
Destructuring assignment of a parametrized lambda fixture is also supported
185+
```python
186+
# test_number_5.py
187+
188+
import pytest
189+
from pytest_lambda import lambda_fixture
190+
191+
lady, where = lambda_fixture(params=[
192+
pytest.param('Monica', 'in my life'),
193+
pytest.param('Erica', 'by my side'),
194+
pytest.param('Rita', 'is all I need'),
195+
pytest.param('Tina', 'is what I see'),
196+
pytest.param('Sandra', 'in the sun'),
197+
pytest.param('Mary', 'all night long'),
198+
pytest.param('Jessica', 'here I am'),
199+
])
200+
201+
def test_your_man(lady, where):
202+
assert lady[:0] in where
203+
```
204+
205+
150206
### Declaring abstract things
151207

152208
`not_implemented_fixture` is perfect for labeling abstract parameter fixtures of test mixins

0 commit comments

Comments
 (0)