我定义了这个函数:
def count_occurrences(cursor, cust_id):
cursor.execute("SELECT count(id) FROM users WHERE customer_id = %s", (cust_id))
total_count = cursor.fetchone()[0]
if total_count == 0:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
我想对它进行单元测试,因为我需要在这里模拟数据库调用。
如何使用 pytest、mock 来完成此操作?
我是初学者。在编译这段代码时
class UpperLowerCase
{
static String ini = "I LOVE JAVA";
static char str[] = ini.toCharArray();
static char invr[] = new char[10];
static int len = 0 , len1 = 0, i;
static void toUpper()
{
for(i=0;i<str.length;i++)
{
if(str[i]>=97 && str[i]<=122)
invr[len1++]=(str[i]-32);
else
invr[len++]=str[i];
}
invr[len1]='\0';
System.out.println("Reverse of"+ str+" is \n"+ invr);
}
static void toLower()
{
for(i=0;i<str.length;i++)
{
if(str[i]>=65 && str[i]<=90)
invr[len1++]=str[i]-32;
else
invr[len++]=str[i];
}
invr[len1]='\0';
System.out.println("Reverse of"+ str+" is \n"+ invr);
}
public static void main(String [] args) …Run Code Online (Sandbox Code Playgroud) 我定义了这个类:
class InternalProc:
@staticmethod
def get_data():
try:
result = subprocess.run(['bridge-client', '--business-credentials'],
stdout=subprocess.PIPE)
data = json.loads(result.stdout.decode('utf-8'))
return data
except Exception:
logger.error("Unable to fetch the data")
raise
Run Code Online (Sandbox Code Playgroud)
我想对 get_data() 进行单元测试,我应该如何模拟 subprocess.run?我还想断言是否引发了异常?
mocking ×2
pytest ×2
python-3.x ×2
database ×1
java ×1
pytest-mock ×1
subprocess ×1
unit-testing ×1