在实际的自动化测试中,创建资源时会要求随机生成资源名,有很多方法,现在列举两种常见方法:

一、根据时间来生成

import time

def get_resource_name(prefix="resource"):
    return prefix + time.strftime ('%m%d_%H%M%S', time.localtime())

生成后的结果如下:

resource0303_134311

 

二、使用random函数来生成

import random

def rand_name(name='', prefix=None):
   
    randbits = str(random.randint(1, 0x7fffffff))
    rand_name = randbits
    if name:
        rand_name = name + '-' + rand_name
    if prefix:
        rand_name = prefix + '-' + rand_name
    return rand_name

有prefix时,结果如下:

qqq-214757399

 

相关文章:

  • 2021-07-17
  • 2021-11-07
  • 2022-12-23
  • 2021-11-17
  • 2021-04-14
  • 2022-12-23
  • 2021-07-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
  • 2022-12-23
  • 2021-12-04
  • 2021-12-31
相关资源
相似解决方案