我使用 py.test 进行测试。
在 setup_class()中,我需要使用 tmpdir 作为我的类构造函数:
class TestMyClass:
def setup_class(self):
self.t = MyClass(path=tmpdir)
def test_test(self):
assert True
我有一个错误:
NameError: name 'tmpdir' is not defined
我无法使用setup_class(self, tmpdir)
。
如果我使用此代码:
def test_needsfiles(tmpdir):
print(tmpdir)
assert 0
这很有用,但我的类构造函数需要 tmpdir 。
怎么做?
谢谢!
UPD
我尝试这样做:
@pytest.yield_fixture()
def constructor(tmpdir):
_t = MyClass(path=str(tmpdir))
yield _t
class TestMyClass:
def test_test(self, constructor):
pass
但我不能在灯具中使用范围:
ScopeMismatch: You tried to access the 'function' scoped fixture 'tmpdir' with a 'module' scoped request object, involved factories
答案 0 :(得分:2)
我这样做:
for (int i = 0; i < loopContent; i++) {
String address = addresses.get(i).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(i).getLocality();
String state = addresses.get(i).getAdminArea();
String country = addresses.get(i).getCountryName();
String postalCode = addresses.get(i).getPostalCode();
String knownName = addresses.get(i).getFeatureName(); // Only if available else return NULL
if (knownName.equals("null") || knownName.toString().isEmpty()) {
knownName = "Нету информации";
}
if (address != null) {
if (address.toString().isEmpty() || address.equals("null")) {
address = "Нету информации";
}
} else {
address = "Нету информации";
}
// holder = new Holder(knownName, address);
// holderArrayList.add(holder);
// adapter.notifyDataSetChanged();
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View inflatedLayout = inflater.inflate(R.layout.single_item, null, false);
TextView header = (TextView) inflatedLayout.findViewById(R.id.header);
TextView street = (TextView) inflatedLayout.findViewById(R.id.second);
header.setText(knownName);
street.setText(address);
layoutToAdd.addView(inflatedLayout);
}
progressBar.setVisibility(View.GONE)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
答案 1 :(得分:0)
如果您不想使用@pytest.fixture(autouse=True)
作为@santon建议,但您想在TestMyClass
之外创建一个夹具(就像您在UPD部分中写的那样),您可以试试这个:< / p>
@pytest.fixture
def t(tmpdir):
return MyClass(tmpdir)
class TestMyClass:
def test_test(self, t):
assert True
如果你不想在夹具中返回任何东西,但是例如转到临时目录,你也可以这样做:
@pytest.fixture
def t(tmpdir):
os.chdir(str(tmpdir))
@pytest.mark.usefixtures("t")
class TestMyClass:
def test_test(self):
assert True
答案 2 :(得分:-2)