Using iMac 2012 as a monitor for MacBook Pro 2012
The answer you are looking for is Target Display mode.

And yes it is compatible with this setup (If they are 2012 machines with Thunderbolt). They must have thunderbolt ports (also work on 27" iMac with Mini Display Port)