25 October 2012

Data and File testing

I put up a few small tests to cover data validation within a file.  I'll be building off this project in Github with examples:
https://github.com/wbwarnerb/ts/tree/master/features

So far I've just got a few tests.  The first is a test that verifies Data Files have been delivered and just makes sure they exist.  It's look at the root of the drive for a few files.  This can be modified easily.  Here's the Scenario File:
Scenario Outline: Check that file exists
Given a file has come in
Then the <datafile> is verified as being there
Examples:
|datafile|
|setup.log|
|hamlet.txt |
 

Here's the Step Definition code:
Then /^the (.*) is verified as being there$/ do |datafile|
  assert File.exists?("/#{datafile}")
end
 
The second test does a count on the lines in each file.  It then assumes the user knows this count and validates that each file is the correct line count.  Here's the code to accomplish it:

Here's the Scenario:
Scenario Outline: Line Count Validation
Given a file has come in
Then the <datafile> and it's <linecount> are verified
Examples:
|datafile|linecount|
|setup.log|10 |
|hamlet.txt |4463 |

Here's the Step Definition Code:
Then /^the (.*) and it's (.*) are verified$/ do |datafile, linecount|
  count = 0
  File.open("/#{datafile}") {|f| count = f.read.count("\n") } == "#{linecount}"
  puts "The line count for '#{datafile}' is:"
  puts count
end

No comments:

Post a Comment