Automated Testing OpenXML SDK

2019-02-09 02:58发布

问题:

I'm implementing ms word document generation using content controls and OpenXML SDK. I'd like to have some automated testing for that code (unit tests or some easy UI automation tests).

Does anyone has expericnce with testing MS Word document generation? What are the possible options?

Thanks in advance!

回答1:

No, I haven't done unit testing of MS Word Document generation, but as Ingó Vals says, it shouldn't be any different from any other form of unit testing.

1) [Optional - to ensure that you understand correct usage of the SDK for your needs]. Work out how your app should drive the SDK. Write some test scripts that mimic intended functionality and ensure that the Word documents they generate meet your expectations.

2) Create an interface (or interfaces) that contain methods that correspond to the functionality that you need for your documentation generation. Note: the interface does not need to offer the full functionality of the OpenXML SDK - only the functionality that you need for your application.

3) Create a concrete implementation of your interface, which forwards calls to the OpenXML SDK

4) Utilise the interface you created in your application to perform document generation.

5) Use NUnit and NMock (or similar) to write unit tests that drive the generation layer of your application. These tests should use a mocked interface, rather than an instance of the concrete implementation. You can now assert in your tests that your generation layer behaves as you expect.



回答2:

I am actually doing something similar with the OpenXML SDK for spreadsheets and I actually just write OpenXML api code that opens the file from a stream for the purpose of testing. Unit Tests don't really tell you enough since you need to know if it is a valid file.

// There should be a sheet for every team
[TestMethod]
[HostType("Moles")]
public void CaseExportTeamSheetsTest()
{
    IRepository<ServiceTbl, ServiceTbl> ServiceRepository;
    CaseController target;
    BuildCaseControllerMoledCases(out ServiceRepository, out target);
    FileStreamResult actual = target.Export();   using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(actual.FileStream, false))
    {
        var services = ServiceRepository.All;

        foreach (var item in services)
        {
            // get a worksheet foreach service
            var sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == item.ServiceName);
            Assert.IsTrue(sheets.Count() > 0);
        }
    }

    actual.FileStream.Close();
    actual.FileStream.Dispose();
}


回答3:

Warning regarding OpenXml Sdk 2.0 and valid code....

I have generated OpenXml Powerpoints documents that validates using XML SDK 2.0 tools and works in Office 2007 on my PC but when opening the document on another machine using Office Powerpoint 2007 it complains and says the format is not valid

XML Sdk 2.0 http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en