As of Bitcoin Core 0.7.1 (19 October 2012), you can run both mainnet and testnet by simply starting Bitcoin Core with the appropriate flags:
bitcoind -daemon # mainnet, -daemon will cause it to fork into the background
bitcoind -daemon -testnet # testnet
As of Bitcoin Core 0.9.0 (19 March 2014), you can easily use
bitcoin-cli to send commands to both nodes:
bitcoin-cli getinfo # mainnet, uses port 8332 for RPC-JSON
bitcoin-cli -testnet getinfo # testnet, uses port 18332 for RPC-JSON
Also as of Bitcoin Core 0.9.0, there is a regression test mode ("regtest") that can speed up development and testing of apps by nearly instantly generating an alternative block chain with super-low difficulty.
By default, regtest mode runs on the same ports as testnet (18333 for Bitcoin P2P, 18332 for RPC-JSON/HTTP REST), although it does use a different data directory from testnet. That means the two commands below will both address a default testnet or regtest node:
bitcoin-cli -testnet getinfo ## sends command to port 18332
bitcoin-cli -regtest getinfo ## also sends command to port 18332
This answer has some basic sample code showing running two regtest nodes on the same machine, but you can also do something similar if you want to run a testnet node and regtest node at the same time.
Post script: Pieter Wuille's accepted answer confuses me. It says he wrote it on 9 December 2012, but this merge by Pieter says he added code (written by @kjj2) changing the testnet port to 18332 on 28 September 2012---over two months earlier. Also, that code was included in Bitcoin Core 0.7.1 released in October 2012, still almost two months earlier than Pieter's answer.
Shrug. I guess maybe he just forgot about the change.