4

I am using unittest and selenium to automate my browser testing.

How would I go about making a test which I can run multiple times, where a user creates a ticket. The ticket has to has a title name, each time I run the test I want the title name to be random.

I would like the format: "Test ticket, 1 | Test ticket, 2..."

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
gallly
  • 1,201
  • 4
  • 27
  • 45
  • What are you having problems with? `Ticket 1`, `Ticket 2`, ... are trivial to create. – Blender May 21 '13 at 22:21
  • The example you showed doesn't seem very random. Also, is that format describing a single ticket, a list of tickets separated by commas, a list of tickets separated by pipes, or… what? Please describe the intended output less vaguely. – abarnert May 21 '13 at 22:26
  • a single ticket, each time i run the test, it should create 1 ticket. There are other steps included but I just want to cover the name part. I am sure its really simple, but I am noob – gallly May 21 '13 at 22:43

3 Answers3

8

The faker module offers some functionality to populate several different types of data:

import faker
f = faker.Faker()

In [11]: f.
f.city            f.full_address    f.phonenumber     f.zip_code
f.company         f.last_name       f.state
f.email           f.lorem           f.street_address
f.first_name      f.name            f.username

In [11]: f.city()
Out[11]: u'Treyview'

.

If you are going to test randomly, I recommend randomly generating a seed (and logging it), that way you can recreate any failing tests. What you don't want is tests which fail but it's unclear why (i.e. if testing again, with different random values, passes).

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • thanks for the advice, I am not sure what you mean randomly generating a seed, could you provide some code for me to look at :). I am guessing that when I get to the ticket form, and send_keys, this is where I will need to have a generator? – gallly May 21 '13 at 22:44
  • @JacobHong I wonder if what you *really* want is something like [Faker](https://pypi.python.org/pypi/Faker/0.0.4)... – Andy Hayden May 21 '13 at 22:49
  • this is more than I wanted, Im studying the documents now thanks :) – gallly May 21 '13 at 22:53
  • @JacobHong I think I'm just oing to change my answer to that... annoyingly it doesn't seem to offer a seed function... yet. – Andy Hayden May 21 '13 at 23:24
  • I put together a python module which has better locale support (using ruby's data), I haven't put it in the cheese shop though: https://github.com/hayd/pyfaker – Andy Hayden Jun 20 '13 at 21:56
2

If you just need the string Test ticket,1 ... it is:

from random import randint
randomString = "Test ticket, " + randint(min,max)

If you want to generate random strings you could use

''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(max))

You also might want to think about preventing strings to be equal. In that case you could create a range(min,max) and use random.shuffle()

kenorb
  • 155,785
  • 88
  • 678
  • 743
Johannes Jasper
  • 861
  • 1
  • 7
  • 30
  • thanks this is pretty much what I was looking for, I want to add on one more question. How can I make the test case, run multiple times? just add a loop or something? – gallly May 21 '13 at 22:47
0

You can define the following functions in your test:

import random, string

def random_word(self, length=6, chars=string.ascii_lowercase):
   return ''.join(random.choice(chars) for i in range(length))

def random_id(self, size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

def random_number(self, length=3):
    return ''.join(random.choice(string.digits) for i in range(length))

and similar.

See also:

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743