/
test.py
61 lines (44 loc) · 1.47 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import inspect
from dataclasses import dataclass
from enum import Enum, auto
from types import MappingProxyType, ModuleType
from typing import Callable, Dict
from ward.fixtures import Fixture, FixtureRegistry
class WardMarker(Enum):
NONE = auto()
SKIP = auto()
XFAIL = auto()
def skip(func):
func.ward_marker = WardMarker.SKIP
return func
def xfail(func):
func.ward_marker = WardMarker.XFAIL
return func
@dataclass
class Test:
fn: Callable
module: ModuleType
marker: WardMarker = WardMarker.NONE
def __call__(self, *args, **kwargs):
return self.fn(*args, **kwargs)
@property
def name(self):
return self.fn.__name__
@property
def qualified_name(self):
return f"{self.module.__name__}.{self.name}"
def deps(self) -> MappingProxyType:
return inspect.signature(self.fn).parameters
def has_deps(self) -> bool:
return len(self.deps()) > 0
def resolve_args(self, fixture_registry: FixtureRegistry) -> Dict[str, Fixture]:
"""Resolve fixture that has been injected into this test"""
if not self.has_deps():
return {}
# Construct a dict of kwargs to pass into the test when it's called
resolved_args = {}
for fixture_name in self.deps():
fixture = fixture_registry[fixture_name]
resolved_arg = fixture.resolve(fixture_registry)
resolved_args[fixture_name] = resolved_arg
return resolved_args