Utilizing your .NET Project's Automated Acceptance Tests on Crystal Reports - Part 2
page 4 of 5
by Eric Landes
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27084/ 72

Writing the code for the Fitnesse test

Now we need to write our code for testing this.  To refresh your memory, we can use different Fitnesse fixtures to run our tests.  In this case as in part 1, we utilize a columnfixture.  In this test, we cannot analyze using the reportdocument object.  We need to analyze the reports contents once it's rendered.  The reportdocument object can export the contents, but we cannot use the reportdocument object directly to test for values.

To get our values for the test we will export the report to a stream, and then read the stream as a string.  Below is the code for doing this:

Listing 1

public class valuesinreport: ColumnFixture
{
  public string fieldname;
  public string value;
  public Boolean ok()
  {
    CrystalReport1 report = new CrystalReport1();
    MemoryStream memReport = (MemoryStream)report.ExportToStream
      (CrystalDecisions.Shared.ExportFormatType.HTML40);
    memReport.Position = 0;
    byte[]byteArray = new byte[memReport.Length];
    int count = memReport.Read(byteArray, 0, (int)memReport.Length);
    String strReport = Encoding.ASCII.GetString(byteArray);
    if (strReport.IndexOf(fieldname) >  - 1)
    {
      //Check if date value
      if (value == "today")
      {
        if (strReport.IndexOf(DateTime.Now.ToShortDateString()) >  - 1)
        {
          return true;
        }
      }
      else
      {
        if (strReport.IndexOf(value) >  - 1)
        {
          return true;
        }
      }
    }
    return false;
  }
}

In this code, we first set the fields for the fieldname, and value.  And as in part 1, we create a method called OK that returns a Boolean.  Fitnesse will pass the fieldname and value to the OK method.  Within the OK method, we get our report (CrystalReport1) named report.  We then need to get the report results back in a stream.  To do that we use the ExportToStream method found in the ReportDocument object.  Here we cast it to a MemoryStream, but it can be cast to any valid Stream type.

We then get the string value of the report, by reading the Stream.  Then we cast the resulting byte array into an ASCII value, pushing into a string variable. In the if statements, this particular test is checking for the presence of the value "today".  That way we know to test against the DateTime.Now value. 

Once we have the report in a string, it's trivial to run the tests.  This simple test shows you how to accomplish this, by using the indexof property.  We are assuming that if the value is present in the report, this is true.  You can create more complex testing if you want, and test that this is after a heading or something.  But that is beyond the scope of this article. 

Once you run this test, your Fitnesse test should run green. Just like the one in Figure 1. And you now have a test to run in your QA build script!

Figure 1


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 



Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 6:41:35 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search