我有一个 pytest 固定装置,只需在所有 pytest 工作人员中运行一次。
@pytest.fixture(scope="session")
@shared # this will call setup once for all processes
def cache(request):
acc = Account(id=10)
acc.create()
request.addfinilizer(acc.delete)
return acc
def shared(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
request = kwargs['request']
root = request.config._tmp_path_factory.getbasetemp().parent
filepath = root / "shared"
with filelock.FileLock(f'{filepath}.lock'):
if filepath.is_file():
result = json.loads(filepath.read_text())
else:
result = func(*args, **kwargs)
filepath.write_text(json.dumps(result.id))
return result
return wrapper
Run Code Online (Sandbox Code Playgroud)
我使用https://pytest-xdist.readthedocs.io/en/latest/how-to.html?highlight=only%20once#making-session-scoped-fixtures-execute-only-once中的解决方案,效果很好对于 pytestsetup部分,但该teardown部分在每个 pytest 进程中都会被调用。
是否可以锁定pytest-xdist拆卸以在所有 pytest 会话完成后仅运行一次?我想为所有工人运行一次拆卸。
这是我在python 3.6中的代码
class A(object)
def __init__(self, a: str):
self._int_a: int = int(a) # desired composition
def get_int_a(self) -> int:
return self._int_a
Run Code Online (Sandbox Code Playgroud)
我想重写这段代码python 3.7,我如何self._int_a: int = int(a)用dataclasses模块初始化?
我知道我可以做类似的事情,但我不知道如何初始化_a: int = int(a)或类似。
from dataclasses import dataclass
@dataclass
class A(object):
_a: int = int(a) # i want to get initialized `int` object here
def get_int_a(self) -> int:
return self._a
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的想法和建议。