2

How can I automate the testing of the printing functionality of my WPF application, including changing common printing properties like paper size, tray, landscape/portrait etc?

I'm thinking about using some sort of virtual printer driver like EmfPrinter, but I still have no idea how to verify, for example, that a paper tray has been set correctly.

avo
  • 10,101
  • 13
  • 53
  • 81
  • 3
    This sounds more like an integration test than a unit test. You could consider encapsulating the access to the printer behind an abstraction. – Nkosi Feb 20 '19 at 01:54
  • This might be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Provide a [mcve] that clarifies your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Nkosi Feb 20 '19 at 01:56
  • @Nkosi, thanks for your thoughts. If I had such an abstraction, I'd have to mock [`PageSettings.PaperSource`](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.pagesettings.papersource?view=netframework-4.7.2) and basically all other printing APIs from `System.Drawing.Printing` that I use for printing. Is that something you suggest? – avo Feb 20 '19 at 10:19
  • 1
    No that is not something I suggest. I suggest that you reviver your current design as it appears to be tightly coupled to implementation concerns that make unit testing in isolation difficult – Nkosi Feb 20 '19 at 10:43

1 Answers1

2

Your testing problem may contain unit-testing aspects, but from the description it appears to (at least also) address integration testing aspects. For example you ask

how to verify, for example, that a paper tray has been set correctly

I read this in the following way: You want to be sure that the printing functionality of your software achieves that on the real physical printers the paper tray has been set correctly. If this understanding is correct, then this goes beyond what you can achieve with unit-testing:

With unit-testing you try to find the bugs in small, isolated software pieces. Isolation is achieved by replacing components that the software-under-test (SUT) depends on. These replacement components are often called doubles, mocks or stubs. With such doubles, however, you can not find those bugs, which are related to misconceptions about how the interaction of the SUT with the depended-on components should work: Since you implement the doubles, you implement them based on your potential misconceptions.

For the printer functionality this means, if you double the paper tray selection function, then you can test certain aspects of the unit, but you will still not know if it interacts correctly with the real paper tray selection function. This is not a flaw of unit-testing: It is just the reason why in addition to unit-testing you also need integration and system testing.

To address all aspects of your testing problem you can make your life easier: Design your code such that you try to separate computations from interactions. You would then test the units (functions, methods, ...) that contain computations with unit-testing. Those parts that contain the interactions you would test with integration testing. There will likely remain code parts where such a separation is difficult and you are left with computations mixed with interactions. Here you may have to do the unit-testing with doubles (ensuring by design that you can inject them - see dependency injection and inversion of control). But, this strategy in general can save you a lot of effort for creation of doubles.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47
  • This is a great answer, thank you. Could you recommend a good book or a course on the subject? – avo Feb 21 '19 at 22:51
  • 1
    Books on unit-testing I like: Osherove, The Art of Unit Testing (good for a start); Feathers, Working Effectively with Legacy Code (design/refactoring for testability); Meszaros, xUnit Test Patterns (high quality test code). I have a book on integration testing in German, but not in English. Don't know a book with focus on how to handle the boundary between unit-testing and integration-testing in practice. Such questions, however, appear frequently on SO: https://stackoverflow.com/q/54016901/5747415; https://stackoverflow.com/q/54564969/5747415; https://stackoverflow.com/q/54529245/5747415 – Dirk Herrmann Feb 22 '19 at 09:26