Test Driven Development for a Business Intelligence Project with Crystal Reports: Part 2
page 4 of 7
by Eric Landes
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26895/ 39

Test Output of Report to Text File

The next unit test checks the grand total of sales generated from a query in the database against the output of the report.  We haven’t changed the report format yet, but will in Part 3 to meet the scenario mentioned.

We need to determine how we capture the output from the report in order to perform the comparison against the SQL query.  In the case of Crystal, I don’t care what format it’s in, but I need to be able to read the report format programmatically.  So I decided to use the text format for output in my test case.

Before I actually test the grand total, I create a test that will generate the report and output it to a text file. After we verify that we can find the grand total, we’ll parse the data and compare the grand total to that calculated from a straight-up SQL query.

Code Listing #1

[Test]

public void SalesByYearReportResults()
{
 
      CrystalObjects.ReportObjects oCrObjs = new CrystalObjects.ReportObjects();
                  oCrObjs.ServerName="(local)";
                  oCrObjs.UserName="NorthWind";
                  oCrObjs.UserPassWord="NorthWind";
                  oCrObjs.Database="NorthWind";
      oCrObjs.LoadReport(@"C:\Dev\UnitTestBICrystal\SalesReportToTest.rpt");
      if (System.IO.File.Exists(@"c:\temp\ReportTest.html"))
      {
            System.IO.File.Delete(@"c:\temp\ReportTest.html");
      }
      oCrObjs.FolderName=@"C:\Temp\";
      oCrObjs.FileName=@"ReportTest.html";
      oCrObjs.ExportData();
      using (StreamReader sr = new StreamReader(@"c:\temp\ReportTest.html")) 
      {
            String line;
            // Read and display lines from the file until the end of 
            // the file is reached.
            while ((line = sr.ReadLine()) != null) 
            {
                  if (line.IndexOf("Total") > 0)
                  {
                        Assert.IsTrue(true,"Not found");
                  }
            }
      }
                  
}

 

[code in CrystalObjects]

public void ExportData() 
{ 
      CrystalDecisions.Shared.DiskFileDestinationOptions oDest 
            = new CrystalDecisions.Shared.DiskFileDestinationOptions(); 
      CrystalDecisions.Shared.ExportOptions oExpOptions 
            = new CrystalDecisions.Shared.ExportOptions();
      string ExportFileName = _fileName; 
      try 
      { 
                  
            oExpOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        oExpOptions.ExportFormatType = ExportFormatType.Text;
                        oDest.DiskFileName=FolderName + FileName;
                        oExpOptions.ExportDestinationOptions=oDest;
                        currDoc.Export(oExpOptions);
      } 
      catch (Exception e) 
      {
            throw new Exception("Error encountered in ExportData: " + e.Message);
      } 
}

The code above starts out using my custom object, CrystalObjects, introduced in Part 1.  The test loads the report and checks for the existence of a file that contains the report results.  Two new properties are now part of CrystalObjects: FileName and FolderName.   I then created the ExportData method which exports a text version of the report to the file and folder that are set in the FileName and FolderName properties.  ExportData will need to be refactored in the future to allow for different export options.  But right now it does what I need it to do: export to a text file.

When I run this test, I see a green bar (indicating a successful test).  That means it’s time to do some actual comparisons.


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-04-24 6:10:07 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search