• java、python及jmeter操作redis(接口自动化必备)


    redis是nosql数据库之一,其存储结构简单,提供高性能服务,所以在架构中是很常用的。

    在做接口自动化测试过程中,有时也会涉及到redis,比如:发送短信验证码,我们做接口自动化测试,如何模拟发送短信验证码呢?

    一般来说,短信验证码都是存储到redis中的,且有过期时间,我们完全可以直接往redis里面插入一个k-v来模拟发送短信验证码,所以,就得学会java、python及jmeter操作redis了。

    java操作redis

    创建项目

    创建maven项目,添加依赖

    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency> 

    连接redis

    System.out.println("---连接redis---");
    Jedis jedis = new Jedis("192.168.168.168",6379);
    jedis.auth("test123");  // redis数据库的密码
    jedis.select(0);  // 哪怕注释这行,也没问题,因为默认就是选择的db0
    jedis.flushDB(); 

    string

    System.out.println("
    ---string---");
    jedis.set("name","jack");
    String name = jedis.get("name");  // 返回的string
    System.out.println("name: " + name);

    结果:

    ---string---
    name: jack

    list

    System.out.println("
    ---list---");
    jedis.lpush("testlist", "aaa", "bbb", "ccc");
    System.out.println(jedis.lrange("testlist",0,-1));
    List<String> keys = jedis.lrange("testlist",0,-1);  // 返回的list
    System.out.println(keys);

    结果:

    ---list---
    [ccc, bbb, aaa]
    [ccc, bbb, aaa]

    set

    System.out.println("
    ---set---");
    jedis.sadd("testset", "a", "b", "c");
    System.out.println(jedis.smembers("testset"));
    Set<String> keys2 = jedis.smembers("testset");  // 返回的set
    System.out.println(keys2);

    结果:

    ---set---
    [b, a, c]
    [b, a, c]

    zset

    System.out.println("
    ---zset---");
    jedis.zadd("testzset",10,"aa");
    jedis.zadd("testzset",20,"bb");
    jedis.zadd("testzset",30,"cc");
    System.out.println(jedis.zrange("testzset", 0, -1));
    Set<String> keys3 = jedis.zrange("testzset", 0, -1);  // 返回的set
    System.out.println(keys3);

    结果:

    ---zset---
    [aa, bb, cc]
    [aa, bb, cc]

    hash

    System.out.println("
    ---hash---");
    jedis.hset("testhash", "name", "lucy");
    jedis.hset("testhash", "age", "18");
    Set<String> keys4 = jedis.hkeys("testhash");  // 返回的set
    System.out.println(keys4);
    for (String key:keys4){
        System.out.println(key+"="+jedis.hget("testhash",key));
    }

    结果:

    [name, age]
    name=lucy
    age=18

    获取所有key

    System.out.println("
    ---获取所有key---");
    Set<String> keys5 = jedis.keys("*");  // 返回的set
    System.out.println(keys5);

    结果:

    ---获取所有key---
    [testlist, name, testzset, testhash, testset]

    关闭连接

    jedis.close();

    python操作redis

    参考:https://gitee.com/UncleYong/my_rf/blob/master/db_operate/redis_operate.py

    jmeter操作redis

    下载jar包,下载地址:https://mvnrepository.com/,获取点击左侧加群获取。

    放到如下目录,即:D:apache-jmeter-5.1.1libext

    清空redis中db0

    beanshell脚本

    import redis.clients.jedis.Jedis;
    
    Jedis jedis = new Jedis("192.168.168.168",6379);
    
    jedis.auth("test123");
    jedis.select(0);
    jedis.hset("mobile:13888088168","vefication","168168");
    jedis.close();

    运行上面脚本,然后通过命令查看添加的结果

    更多redis命令,参考:https://www.cnblogs.com/UncleYong/p/10950727.html

  • 相关阅读:
    Implement JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1 Part 3 (by TAISEER)
    The OAuth 2.0 Authorization Framework: Bearer Token Usage
    Token Based Authentication using ASP.NET Web API 2, Owin, and Identity
    JSON Web Token in ASP.NET Web API 2 using Owin
    Understanding and Creating OWIN Middlewares
    What is OWIN? A Beginners Guide
    two pointers
    ASP.NET Web API Claims Authorization with ASP.NET Identity 2.1 Part 5 (by TAISEER)
    OWIN OAuth 2.0 Authorization Server
    AccessToken-->Password Grant
  • 原文地址:https://www.cnblogs.com/uncleyong/p/10950733.html
Copyright © 2020-2023  润新知