小编use*_*934的帖子

使用 pytest-mock 在 python 中模拟数据库调用

我定义了这个函数:

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 来完成此操作?

database unit-testing mocking pytest python-3.x

7
推荐指数
1
解决办法
1万
查看次数

如何解决从 int 到 char 的错误有损转换?

我是初学者。在编译这段代码时

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)

java

6
推荐指数
1
解决办法
3万
查看次数

如何在 pytest 中模拟 subprocess.run?

我定义了这个类:

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?我还想断言是否引发了异常?

subprocess mocking pytest python-3.x pytest-mock

1
推荐指数
1
解决办法
1007
查看次数