如何为我的Python脚本生成测试数据?

3
一个方程的取值形式如下:
   x = [0x02,0x00]  # which is later internally converted to in the called function to  0x300
   y = [0x01, 0xFF]
   z = [0x01, 0x0F]

我该如何为这个函数生成一系列的测试值? 例如,我想从一个for循环中发送100个奇数值。

for i in range(0,300):
   # where a,b are derived for a range
   x = [a,b]

我的问题表述不够清晰,请允许我澄清一下。我想问如何生成不同的值来代替 a,b,使得 x =[a,b] 有所变化。

2个回答

3

使用生成器:

def gen_xyz( max_iteration ):
    for i in xrange( 0, max_iteration ):
       # code which will generate next ( x, y, z )
       yield ( x, y, z ) 

for x, y, z in gen_xyz( 1000 ):
  f( x, y, z )

1

hex() 函数?

import random
for i in range(10):
    a1, a2 = random.randint(1,100), random.randint(1,100)
    x = [hex(a1), hex(a2)]
    print x

..输出类似于..

['0x21', '0x4f']
['0x59', '0x5c']
['0x61', '0x40']
['0x57', '0x45']
['0x1a', '0x11']
['0x4c', '0x49']
['0x40', '0x1b']
['0x1f', '0x7']
['0x8', '0x2b']
['0x1e', '0x13']

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接