Skip to content

Commit 018dc1c

Browse files
committed
Refactor pytest fixtures
1 parent 9454ef4 commit 018dc1c

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

tests/conftest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import uuid
23
from pathlib import Path
34

45
import pytest
@@ -95,3 +96,35 @@ def restart_api():
9596
).touch()
9697
time.sleep(0.5)
9798
wait_for_webapp_to_come_up()
99+
100+
101+
@pytest.fixture
102+
def random_suffix():
103+
def _random_suffix():
104+
return uuid.uuid4().hex[:6]
105+
106+
return _random_suffix
107+
108+
109+
@pytest.fixture
110+
def random_sku(random_suffix):
111+
def _random_sku(name=""):
112+
return f"sku-{name}-{random_suffix()}"
113+
114+
return _random_sku
115+
116+
117+
@pytest.fixture
118+
def random_batchref(random_suffix):
119+
def _random_batchref(name=""):
120+
return f"batch-{name}-{random_suffix()}"
121+
122+
return _random_batchref
123+
124+
125+
@pytest.fixture
126+
def random_orderid(random_suffix):
127+
def _random_orderid(name=""):
128+
return f"order-{name}-{random_suffix()}"
129+
130+
return _random_orderid

tests/e2e/test_api.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
1-
import uuid
2-
31
import pytest
42
import requests
53

64
pytestmark = pytest.mark.e2e
75

86

9-
def random_suffix():
10-
return uuid.uuid4().hex[:6]
11-
12-
13-
def random_sku(name=""):
14-
return f"sku-{name}-{random_suffix()}"
15-
16-
17-
def random_batchref(name=""):
18-
return f"batch-{name}-{random_suffix()}"
19-
20-
21-
def random_orderid(name=""):
22-
return f"order-{name}-{random_suffix()}"
23-
24-
257
@pytest.mark.usefixtures("postgres_db")
268
@pytest.mark.usefixtures("restart_api")
27-
def test_add_batch(url):
9+
def test_add_batch(url, random_sku, random_batchref):
2810
sku, othersku = random_sku(), random_sku("other")
2911
earlybatch = random_batchref(1)
3012
laterbatch = random_batchref(2)
@@ -53,7 +35,9 @@ def test_add_batch(url):
5335

5436

5537
@pytest.mark.usefixtures("restart_api")
56-
def test_returns_200_and_allocated_batch(url, post_to_add_stock):
38+
def test_returns_200_and_allocated_batch(
39+
url, post_to_add_stock, random_sku, random_batchref, random_orderid
40+
):
5741
sku, othersku = random_sku(), random_sku("other")
5842
earlybatch = random_batchref(1)
5943
laterbatch = random_batchref(2)
@@ -71,7 +55,9 @@ def test_returns_200_and_allocated_batch(url, post_to_add_stock):
7155

7256

7357
@pytest.mark.usefixtures("restart_api")
74-
def test_retuns_400_and_out_of_stock_message(url, post_to_add_stock):
58+
def test_retuns_400_and_out_of_stock_message(
59+
url, post_to_add_stock, random_sku, random_batchref, random_orderid
60+
):
7561
sku, small_batch, large_order = (
7662
random_sku(),
7763
random_batchref(),
@@ -87,7 +73,7 @@ def test_retuns_400_and_out_of_stock_message(url, post_to_add_stock):
8773

8874

8975
@pytest.mark.usefixtures("restart_api")
90-
def test_returns_400_invalid_sku_message(url):
76+
def test_returns_400_invalid_sku_message(url, random_sku, random_orderid):
9177
unknown_sku, orderid = random_sku(), random_orderid()
9278

9379
data = {"orderid": orderid, "sku": unknown_sku, "qty": 20}
@@ -98,7 +84,9 @@ def test_returns_400_invalid_sku_message(url):
9884

9985

10086
@pytest.mark.usefixtures("restart_api")
101-
def test_deallocate(url, post_to_add_stock):
87+
def test_deallocate(
88+
url, post_to_add_stock, random_sku, random_batchref, random_orderid
89+
):
10290
sku, order1, order2 = random_sku(), random_orderid(), random_orderid()
10391
batch = random_batchref()
10492
post_to_add_stock(batch, sku, 100, "2011-01-01")

0 commit comments

Comments
 (0)