问题描述
我有一些装置,它们只返回类的对象。这些对象包含我要在pytest中验证的该类的属性,因此代码看起来像这样:
//----------------------------------------------
// This function converts n bytes Binary (up to 8,but can be any size)
// value to n bytes BCD value or more.
//----------------------------------------------
void bin2bcdn(void * val,unsigned int8 cnt)
{
unsigned int8 sz,y,buff[20]; // buff = malloc((cnt+1)*2);
if(cnt > 8) sz = 64; // 8x8
else sz = cnt * 8 ; // Size in bits of the data we shift
memset(&buff,sizeof(buff)); // Clears buffer
memcpy(&buff,val,cnt); // copy the data to buffer
while(sz && !(buff[cnt-1] & 0x80)) // Do not waste time with null bytes,{ // so search for first significative bit
rotate_left(&buff,sizeof(buff)); // Rotate until we find some data
sz--; // Done this one
}
while(sz--) // Anyting left?
{
for( y = 0; y < cnt+2; y++) // Here we fix the nibbles
{
if(((buff[cnt+y] + 0x03) & 0x08) != 0) buff[cnt+y] += 0x03;
if(((buff[cnt+y] + 0x30) & 0x80) != 0) buff[cnt+y] += 0x30;
}
rotate_left(&buff,sizeof(buff)); // Rotate the stuff
}
memcpy(val,&buff[cnt],cnt); // copy the buffer to the data
// free(buff); //in case used malloc
} // :D Done
from main import My_class
import pytest
@pytest.fixture()
def fixt1():
object1 = My_class("atr1","atr2")
return object1
@pytest.fixture()
def fixt2():
object2 = My_class("atr3","atr4")
return object2
@pytest.mark.parametrize('inputs,results',[
(fixt1,10.0),(fixt2,22.5)
]
)
def test_fixts(inputs,results):
assert inputs.price == results
但是,尝试使用这样的简单测试来测试这些属性将起作用:
AttributeError: 'function' object has no attribute 'price'
由于找不到任何有关如何在参数化中正确调用对象属性的说明,我可以就此问题获得一些建议吗?
非常感谢!
解决方法
夹具不能像函数一样使用-必须在测试函数或其他夹具中用作参数。在mark.parametrize
的参数中,您可以改用普通函数:
def param1():
return My_class("atr1","atr2")
def param2():
return My_class("atr3","atr4")
@pytest.mark.parametrize('inputs,results',[
(param1(),10.0),(param2(),22.5)
]
)
def test_fixts(inputs,results):
assert inputs.price == results
请注意,您只能使用可以在加载时执行的功能,因为mark.parametrize
装饰器是在加载时评估的,因此它不能依赖于任何运行时更改。