0

After recently getting to know Catch2, I would very much like to use it to test some functionality of a wxWidgets project. All of the simple examples I have seen run basically as a console app. Others I have seen, are used to instantiate and test a full class. There are some examples which show how to set things up to allow the user to create his own main() function, but I have not found an example which shows me how to incorporate the works into my app.

What I think I need is to find way so that I can pass in a pointer to the main frame class which would allow me to test member functions.

If I can also start the test from either a menu or tool bar and hopefully capture the output for logging or display in the GUI, that would be even better.

At this stage I have no working code at all. Though I have looked at the tests within the wxWidgets distribution for 3.1.3 and found some use of Catch2, but nothing that I could find, covers what I think I need.

Running with wxWidgets 3.1.3 under Win 10 & using MSVC 2019

Stacker
  • 27
  • 3

1 Answers1

1

It's not really clear what are you asking, but wxWidgets own unit test suite uses CATCH (albeit v1, and not v2, but they're similar enough for this not to matter) and you can see how it uses wxUIActionSimulator to emulate user actions.

VZ.
  • 21,740
  • 3
  • 39
  • 42
  • My search through wxWidgets tests showed that Catch was used to test individual wxWidgets classes. In my case, while not exactly legacy code, the 'business' logic is typically contained in functions which are a member of the main frame class. Hence, to test these functions, without turning them into some sort of external C-like functions, it seems that they will need a pointer to the main frame, so that they can be called directly. In another previous project, several years in the past, (using cppUnit & wxTestRunner) this was accomplished using a global pointer to the main frame – Stacker Aug 13 '20 at 16:11
  • You do need to create your main frame, of course, but this goes without saying. Once this is done you can use `wxUIActionSimulator` to e.g. select the appropriate menu items and verify that they result in the expected things happening. – VZ. Aug 13 '20 at 19:29
  • I'll have to give that a try. – Stacker Aug 13 '20 at 21:08
  • So I add UT menu items to my main frame and the handlers for those can then use TEST_CASE & REQUIRE? Assuming of course that have set things up appropriately for Catch2 without having Catch2 generate its own main. – Stacker Aug 13 '20 at 21:13
  • Normally I'd test normal menu items, if you add special menu items, I don't see how is it better than just extracting the function to test and calling it directly. – VZ. Aug 14 '20 at 12:09
  • I don't understand how this extraction of the function of interest would work in way that keep all the relevant code the same for either UT or regular work. The wxWidgets example seems quite involved, partly because of the effort to support multiple platforms. By now, IMO, Catch2 may well be easy to use in some environments, but if I can't make it work without spending weeks in mine, I'll go back to cppunit and wxTestRunner :-( – Stacker Aug 14 '20 at 15:51
  • Sorry, I just don't understand what your problem is. You extract a function and then call it from either your event handlers used during normal runs of the program or directly when testing. If you don't want to do this, you synthesize the events when testing. It's really not difficult. A more difficult part is checking that the UI is in the expected state later, but this is a completely different question. – VZ. Aug 14 '20 at 17:10
  • It seems my problem is not understanding the implementation of the 'extract' a function from a class. Can you point to an example in any of the wxWidgets code - either in regular code or the test code where this principle has been applied to illustrate this operation? – Stacker Aug 14 '20 at 22:50
  • "Extract a function" is just a simple refactoring when you make your event handler call a separate function which can be called from your tests too. – VZ. Aug 15 '20 at 10:15
  • But that 'extracted' function would still end up as a member of the main frame and hence I would have to somehow connect the Catch2 code to the main frame. That is where I am stuck. – Stacker Aug 15 '20 at 19:02
  • Too bad this question has been left hanging. I am looking at the same issue and have not found any satisfactory resolution. A secondary question: the current wsWidgets (3.1.5) code still uses the older Catch framework, Is there an update to the current Catch2 framework under development? ETA? – user999713 Aug 16 '21 at 21:32
  • CATCH2 is mostly backwards compatible with CATCH, there should be no problems with using it, but why should you care which version does wxWidgets use for its own tests? Of course, nothing whatsoever prevents you from using CATCH2, or even 3, for your own tests. – VZ. Aug 17 '21 at 17:03
  • The thing that does prevent me from using Catch2 is not having a pattern to follow for adapting what the wxWidgets code uses to my own wxWidgets app. – user999713 Aug 17 '21 at 19:32
  • Apparently I was too slow in clarifying my last comment: Aside from the older version used in the wxW tests, the code used there does not seem to explain how I would adapt either version to an existing app, without starting over. At least I have not found any other examples and the wxW code (IMO) differs significantly from any other tutorial or explanation I have been able to find. – user999713 Aug 17 '21 at 19:43
  • The best way to test remains separating your business logic from your GUI code in order to allow to test as much of non-trivial code as possible using simple "classic" unit tests. For testing GUI code you can use `wxUIActionSimulator` and wx GUI tests do show how to do it, but testing GUIs is more difficult. – VZ. Aug 17 '21 at 23:04