Forum Discussion

blbdt36's avatar
blbdt36
Contributor
9 years ago
Solved

Using Text files for testing data in a script

Is there any way to use a text file for testing data in a script?  For example, I want to have a file that will show the following:

Field Name, Testing Area, Expected Value

Field 1, value, "foo"

Field 2, value, "bar"

Field 3, value, "baz"

 

In a specific function, I want to find the corresponding field name and load the data to run the test.  So, when I run field2Validation(testingarea, expected), I want to be able to pass the second line in the file.  I don't want to specify a line number, either.

 

Can this be done?  Can anyone direct me to any information on how to do this?  I did a search myself, but I can only find how to dump the whole file, not line by line.

 

I am thinking an array or possibly a new object might be the best, but I can't figure out how to get the information out of the file.

  • This is what I ended up doing (with a little help from a developer).  I created a function that will read the file all in one go (with some help from the KeyWord Test to Script translations.  As it reads the file, it uses an array to hold each line as an object.  The first column was the object name and the others were data values I want to check.

     

    When I call the array object variable, I just need to name the correct object to get the data I need for that test case.  And make sure that all the functions include //USEUNIT and my parsing function

     

    What I did is below:

    Example file:

    FieldName    Value    Color

    name             190        green

    amount          0             red

     

     

    Run the file through the following function in the main script:

     

     

    var testcases =[];
    var fieldname = '';
    
    function parseInputFile()
    {
    Project.Variables.inputfile.Reset() ;
    
    for(; !Project.Variables.inputfile.IsEOF() ; )
    {
    fieldname = Project.Variables.inputfile.Value("FieldName") ;
    testcases[fieldname]={Value: Project.Variables.inputfile.Value("Value"), Color: Project.Variables.inputfile.Value("Color") };
    Project.Variables.inputfile.Next() ;
    }
    
    }

     

     

     

    Then, I just reference the object I want in my function

     

     

    //USEUNIT ParseFile
    
    function testName()
    {
      Log.Message(testcases['name'].Value);  //this will equal 190
    
    }

     

     

     

5 Replies

    • blbdt36's avatar
      blbdt36
      Contributor

      Wow - that is really complicated.  I wasn't planning on writing 3 functions just to read a file.  That also seems like it will have to run every time I want to access the file.  I don't want that at all.  

       

      I think I mis-worded my original request.  I do want to dump the file, but not on the screen as I saw when originally searching.  I want it into an array/object that I can access through out the project.  So, run a parsing function that dumps the data into a variable and access that data in other functions as I run through all my tests.

       

       

      • blbdt36's avatar
        blbdt36
        Contributor

        This is what I ended up doing (with a little help from a developer).  I created a function that will read the file all in one go (with some help from the KeyWord Test to Script translations.  As it reads the file, it uses an array to hold each line as an object.  The first column was the object name and the others were data values I want to check.

         

        When I call the array object variable, I just need to name the correct object to get the data I need for that test case.  And make sure that all the functions include //USEUNIT and my parsing function

         

        What I did is below:

        Example file:

        FieldName    Value    Color

        name             190        green

        amount          0             red

         

         

        Run the file through the following function in the main script:

         

         

        var testcases =[];
        var fieldname = '';
        
        function parseInputFile()
        {
        Project.Variables.inputfile.Reset() ;
        
        for(; !Project.Variables.inputfile.IsEOF() ; )
        {
        fieldname = Project.Variables.inputfile.Value("FieldName") ;
        testcases[fieldname]={Value: Project.Variables.inputfile.Value("Value"), Color: Project.Variables.inputfile.Value("Color") };
        Project.Variables.inputfile.Next() ;
        }
        
        }

         

         

         

        Then, I just reference the object I want in my function

         

         

        //USEUNIT ParseFile
        
        function testName()
        {
          Log.Message(testcases['name'].Value);  //this will equal 190
        
        }