Added a context manager to capture queries while testing.

Also made some import cleanups while I was there.

Refs #10399.