Skip to content

2.2. File handling

Goal

  • Create a new test data file named students.txt which contains two names in separate lines.
  • Create a new keyword named Read Names From File which will log the names to console from the students.txt.
  • Create a new test case named Students which will utilize the previously created keyword.

Solution

Hints

You can handle files and directories by using keywords from the OperatingSystem library.

Click here to learn more about the OperatingSystem library.

You will need to use Split To Lines keyword from the String library.

Click here to learn more about the String library.

Solution: tests/02-classroom/test_data/students.txt
1
2
Jane Doe
John Doe
Solution: tests/02-classroom/resources/lists.resource
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
*** Keywords ***
Print Multiple Names
    [Arguments]    ${multiple_names}
    Log To Console    ${\n}    no_newline=True
    FOR    ${name}    IN    @{multiple_names}
        Log To Console    ${name}
    END

Read Names From File
    [Arguments]    ${file_with_names}
    ${file_content}=    Get File    ${file_with_names}
    @{names}=    Split To Lines    ${file_content}
    [Return]    ${names}

*** Settings ***
Library    OperatingSystem
Library    String
Solution: tests/02-classroom/01-lists.robot
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
*** Test Cases ***
Greetings Everyone
    [Tags]    loop
    [Documentation]    This test case verifies the functionality of 'Print Multiple Names' keyword.
    Print Multiple Names    ${MULTIPLE_NAMES}

Students
    [Documentation]    This test case logs the names from students.txt to console.
    @{students}=    Read Names From File    ${STUDENTS_TXT}
    Print Multiple Names    ${students}

*** Settings ***
Resource    ${CURDIR}${/}resources${/}lists.resource

*** Variables ***
@{MULTIPLE_NAMES}    Jane Doe    John Doe
${STUDENTS_TXT}      ${CURDIR}${/}test_data${/}students.txt

Results

Inside the tests folder, execute the following command to execute 02-Classroom suite.

1
robot --suite 02-classroom .

You can check the generated log.html file to see how your test cases worked.


Last update: June 22, 2020