0

So I'm facing something of a face off between the perfectionist vs the agile efficiency engineer in me.

I've got an Azure Function which will be using the Open XML SDK to perform some rudimentary functions - for example it adds comments into a Word document.

For simplicity I've wrapped up and modularised all my Open XML SDK code into a separate internal API so I can call methods like:

WordDocuments.AddComment(documentStream, "I'm looking for this text", "Author", "This is my comment");

I have extensive unit test coverage across the majority of the project so far, from individual internal function-level unit tests, right up to post-deployment end-to-end integration testing of the Azure Function endpoints.

However, I'm facing a bit of a quandary facing this particular one.

An obvious set of test steps might be:

  1. Submit a document (via a Memory Stream)
  2. Call the "AddComment" function
  3. Assert that the comment has been added to the updated stream
  // ARRANGE
  var fileName = $@"{Directory.GetCurrentDirectory()}\Documents\Word.docx";
  var bytes = File.ReadAllBytes(fileName);
  using (var stream = new MemoryStream())
            {
               stream.Write(bytes, 0, bytes.Length);

  
               // ACT
               WordDocuments.AddComment(stream,
                    "the text we are looking for"
                    "Martin Hatch", // author of the comment
                    "MH", // initials for the author
                    "the comment text");
            }

            // ASSERT
            Assert.IsTrue(WordDocuments.GetComments(stream).Any(c => c.Author == "Martin Hatch" && c.Text == "the comment text"));

        }

However - this leaves me in a bit of a pickle. I can't "assert" that the comment has been added without using the Open XML SDK itself.

Now - it seems utterly pointless to write a whole bunch of separate Open XML SDK code, when I've already wrapped it all up in my "WordDocuments" API class.

However - should I be using my own API to assert actions, which are using that very same API??

This feels like a bit of an internal loop/testing fail.

So I suppose the real question would be:

How do you test code, when the assertion requires the exact same code that you are testing?

EDIT - I've actually updated the main title now as I feel it makes more sense

Original Title: "How would you go about testing code using the Open XML SDK?"

Martin Hatch
  • 279
  • 2
  • 16

1 Answers1

0

Ok so this took me a little while but I think I've found an answer which I'm comfortable with (although there is still a little bit of test smell going on)

If you have alternatives then please feel free to chime in, the more the merrier!

So this is what I have done.

Step 1 - I've created a bunch of pre-determined Word documents.

  • Document without any comments
  • Document with one comment
  • Document with three comments

I've then created Unit Tests to retrieve the comments from the document, and confirm that we have the correct number of comments.

This effectively PROVES that the "GetComments()" method is working correctly

Step 2 - Create Unit Tests which add one or more comments to those source "pre-determined" word document files.

We can then use GetComments() (which we have already proven is working as expected) to confirm whether the comment we were trying to add was working or not.


The final step is of course to elaborate on all of the variations (trying to add comments with missing arguments, providing non-Word documents, and the like) in order to provide a fuller set of code coverage.

Summary

The take away I suppose here is that each Unit Test does not have to be provable in isolation. It is the collection of tests that provide that overall confidence.

It is perfectly ok to use one part of my API to test another part of my API, as long as all parts are covered by their own separate tests so that faults can be correctly identified - without too many false-negatives .. or worse.. false-positives!

Martin Hatch
  • 279
  • 2
  • 16