With the code, forms and data inside the same database I am wondering what are the best practices to design a suite of tests for a Microsoft Access application (say for Access 2007).
One of the main issues with testing forms is that only a few controls have a hwnd
handle and other controls only get one they have focus, which makes automation quite opaque since you cant get a list of controls on a form to act on.
Any experience to share?
I would design the application to have as much work as possible done in queries and in vba subroutines so that your testing could be made up of populating test databases, running sets of the production queries and vba against those databases and then looking at the output and comparing to make sure the output is good. This approach doesn't test the GUI obviously, so you could augment the testing with a series of test scripts (here I mean like a word document that says open form 1, and click control 1) that are manually executed.
It depends on the scope of the project as the level of automation necessary for the testing aspect.
If your interested in testing your Access application at a more granular level specifically the VBA code itself then VB Lite Unit is a great unit testing framework for that purpose.
I have not tried this, but you could attempt to publish your access forms as data access web pages to something like sharepoint or just as web pages and then use an tool such as selenium to drive the browser with a suite of tests.
Obviously this is not as ideal as driving the code directly through unit tests, but it may get you part of the way. good luck
1. Write Testable Code
First, stop writing business logic into your Form's code behind. That's not the place for it. It can't be properly tested there. In fact, you really shouldn't have to test your form itself at all. It should be a dead dumb simple view that responds to User Interaction and then delegates responsibility for responding to those actions to another class that is testable.
How do you do that? Familiarizing yourself with the Model-View-Controller pattern is a good start.
It can't be done perfectly in VBA due to the fact that we get either events or interfaces, never both, but you can get pretty close. Consider this simple form that has a text box and a button.
In the form's code behind, we'll wrap the TextBox's value in a public property and re-raise any events we're interested in.
Now we need a model to work with. Here I've created a new class module named
MyModel
. Here lies the code we'll put under test. Note that it naturally shares a similar structure as our view.Finally, our controller wires it all together. The controller listens for form events and communicates changes to the model and triggers the model's routines.
Now this code can be run from any other module. For the purposes of this example, I've used a standard module. I highly encourage you to build this yourself using the code I've provided and see it function.
So, that's great and all but what does it have to do with testing?! Friend, it has everything to do with testing. What we've done is make our code testable. In the example I've provided, there is no reason what-so-ever to even try to test the GUI. The only thing we really need to test is the
model
. That's where all of the real logic is.So, on to step two.
2. Choose a Unit Testing Framework
There aren't a lot of options here. Most frameworks require installing COM Add-ins, lots of boiler plate, weird syntax, writing tests as comments, etc. That's why I got involved in building one myself, so this part of my answer isn't impartial, but I'll try to give a fair summary of what's available.
AccUnit
VB Lite Unit I can't say I've personally used it. It's out there, but hasn't seen an update since 2005.
xlUnit xlUnit isn't awful, but it's not good either. It's clunky and there's lots of boiler plate code. It's the best of the worst, but it doesn't work in Access. So, that's out.
Build your own framework
I've been there and done that. It's probably more than most people want to get into, but it is completely possible to build a Unit Testing framework in Native VBA code.
Rubberduck VBE Add-In's Unit Testing Framework
Disclaimer: I'm one of the co-devs.
I'm biased, but this is by far my favorite of the bunch.
3. Start writing tests
So, back to our code from section 1. The only code that we really needed to test was the
MyModel.Reversed()
function. So, let's take a look at what that test could look like. (Example given uses Rubberduck, but it's a simple test and could translate into the framework of your choice.)Guidelines for Writing Good Tests
I know that answer was a little long, and late, but hopefully it helps some people get started in writing unit tests for their VBA code.
I've taken a page out of Python's doctest concept and implemented a DocTests procedure in Access VBA. This is obviously not a full-blown unit-testing solution. It's still relatively young, so I doubt I've worked out all the bugs, but I think it's mature enough to release into the wild.
Just copy the following code into a standard code module and press F5 inside the Sub to see it in action:
Copying, pasting, and running the above code from a module named Module1 yields:
A few quick notes:
Eval
which is a function in the Access.Application object model; this means you could use it outside of Access but it would require creating an Access.Application object and fully qualifying theEval
callsEval
to be aware ofDespite its limitations, I still think it provides quite a bit of bang for your buck.
Edit: Here is a simple function with "doctest rules" the function must satisfy.
Another advantage of Access being a COM application is that you can create an .NET application to run and test an Access application via Automation. The advantage of this is that then you can use a more powerful testing framework such as NUnit to write automated assert tests against an Access app.
Therefore, if you are proficient in either C# or VB.NET combined with something like NUnit then you can more easily create greater test coverage for your Access app.