Bitcoin 0.9 and later include support for Regression Test Mode (aka RegTest mode). RegTest mode creates a single node Bitcoin "network" that can confirm blocks upon command. (RegTest mode can also be used to create small, multi-node networks and even to simulate blockchain reorganizations.)
For example the following command will generate 101 blocks
./bitcoin-cli -regtest generate 101
And yes, you get the newly mined coins. They can’t be spent anywhere, but they’re great for testing.
The best documentation of RegTest mode that I’ve seen so far is How to test applications on the new bitcoinj website.