Robot framework: Using faker to generate a prefix

2019-05-31 09:01发布

问题:

I'm writing test cases that test the functionality of adding an account. This means that I need to generate data for the Account's: Name, email, reference no and order no. I was thinking that for each of these fields I could just generate a random word or number with a prefix so that I can use a script to delete these entries from the database regularly.

The issue is, I'm not sure how to add prefixes or indeed just use most of faker's keywords.

At the minute I'm using the md5 keyword to create a string that I can use for each instance of my test case, I've done so by creating a variable in my resource file:

*** Variables ***
${md5}                MD 5

I then call this variable whenever I wish to write my prefix (I call it at the end of various fields eg. email: email+${md5}@gmail.com, reference: test ${md5} etc

*** Keywords ***
Write username
    Input Text    a11y-username    test ${md5}

I'm not sure where the actual documentation is for using faker from within Robot Framework, I'm using http://fake-factory.readthedocs.org/en to find the providers I want to use and then struggle to get them working from within RF.

Can anyone either help me get random_int() working, or point me to the relevant documentation for ALL faker providers in RF.

Thanks in advance.

回答1:

Overview

Using faker keywords requires nothing more than to call them, and save the results in a variable. You cannot use faker keywords in the variable table, you need to use them within a testcase or keyword. However, you can directly call the faker commands from with a python variable file.

To get an address, for example, you would call the Address keyword. Because the faker keywords are so generic, I recommend fully qualifying the keywords to make it clear you're generating fake data.

For example:

*** Settings ***
| Library | FakerLibrary | WITH NAME | faker

*** Test Cases ***
| Example of using faker
| | ${address}= | faker.Address
| | log | address: ${address}

Using the Random Int keyword

To get a random integer, use the Random Integer keyword:

| Example of using faker to get a random integer
| | ${number}= | faker.Random Int
| | log | my number is ${number}

Initializing variables for the whole suite

If you want to use the same values for an entire suite, you can write a keyword that sets some suite-level variables using the Set Suite Variable keyword.

For example:

*** Settings ***
| Library | FakerLibrary | WITH NAME | faker
| Suite Setup | Initialize Test Data

*** Test Cases ***
| Example of using faker to initialize suite variables
| | log | The suite address is ${address}
| | log | The suite md5 is ${md5}
| | log | The suite number is ${number}


*** Keywords ***
| Initialize test data
| | ${address}= | faker.Address
| | ${md5}= | faker.MD5
| | ${number}= | faker.Random Int
| | 
| | Set suite variable | ${address}
| | Set suite variable | ${md5}
| | Set suite variable | ${number}

Documentation

FakerLibrary keyword documentation is available at https://guykisel.github.io/robotframework-faker/.