Cockpen is a parish in Midlothian, Scotland, containing at its north-west corner the town of Bonnyrigg, which lies two miles (3.2 km) south-west of Dalkeith. It is bounded on the west and north by the parish of Lasswade, on the east, by Newbattle and on the south by Carrington. It extends about three miles (4.8 km) from north to south and its greatest breadth is about 2+1⁄2 miles (4.0 km). The parish church is one mile (1.6 km) south-east of Bonnyrigg on rising land on the west bank of Dalhousie Burn. The building dates from 1820 and replaced an earlier structure.