我正在使用pytest固定装置为测试提供自定义对象。我希望每个测试都有一个新对象,因为某些测试会修改该对象。
from gen3release.filesys import io
import pytest
from gen3release.config import env
import json
from ruamel.yaml import YAML
import hashlib
import os
import copy
@pytest.fixture(scope="function")
def env_obj():
return env.Env("./data/test_environment.$$&")
默认范围是function,这意味着该对象应该为每个测试给我一个新对象。
我对名为io.py的模块进行了5次测试
我运行此测试时
def test_store_environment_params(env_obj, loaded_env_obj):
io.store_environment_params("./data/test_environment.$$&", env_obj,"manifest.json")
sowers = env_obj.sower_jobs
expected_sower = loaded_env_obj.sower_jobs
assert expected_sower == sowers
env_params = env_obj.ENVIRONMENT_SPECIFIC_PARAMS
expected_params = loaded_env_obj.ENVIRONMENT_SPECIFIC_PARAMS
assert expected_params["manifest.json"] == env_params["manifest.json"], f"Got: {env_params}"
io.store_environment_params("./data/test_environment.$$&/manifests/hatchery/", env_obj,"hatchery.json")
assert expected_params["hatchery.json"] == env_params["hatchery.json"]
此测试之前
def test_merge_json_file_with_stored_environment_params(env_obj, loaded_env_obj):
os.system("cp ./data/test_manifest.json ./data/manifest.json")
# h1, j1 = io.read_manifest("./data/manifest.json")
env_params = env_obj.ENVIRONMENT_SPECIFIC_PARAMS["manifest.json"]
print(env_params)
io.merge_json_file_with_stored_environment_params("./data", "manifest.json", env_params, env_obj, loaded_env_obj)
with open("./data/manifest.json") as f:
with open("./data/testmerge_manifest.json") as f2:
assert f2.read() == f.read()
os.system("rm ./data/manifest.json")
此测试失败,因为该测试收到的env_obj已被修改。如何让pytest每次给我一个新对象?