(Reuters) - Mexico will elect 12 new state governors on Sunday, in a key test of the ruling party's ability to withstand public discontent over corruption, impunity and gang violence ahead of the 2018 presidential elections.

President Enrique Pena Nieto's Institutional Revolutionary Party, or PRI, holds nine of the 12 governorships, while the center-right National Action Party (PAN) and the leftist Party of the Democratic Revolution (PRD), jointly have three.

GOVERNORSHIPS

The PRI appeared unlikely to keep all nine states, facing a particularly tough battle in the state of Veracruz.

Polls suggested the PRI was favorite to win at least four states, Sinaloa, Hidalgo, Chihuahua and Quintana Roo, and was trailing behind the PAN in Puebla. The PRI had a slight advantage in Zacatecas and Durango.

The PAN and the PRD are locked in tight races with the PRI in Aguascalientes, Tlaxcala, Oaxaca, and Tamaulipas, but a loss for the PRI in Veracruz would give a boost to the opposition going into 2018.

The PRI has never lost Veracruz, the third most populous state, or Tamaulipas, Durango, Hidalgo and Quintana Roo in gubernatorial contests.

The PRI had never lost any governorship until 1989.

The 12 states up for grabs, out of total of 31, represent approximately 30 percent of the population.

Polls showed a virtual tie in Veracruz between the PRI and its smaller Green Party and New Alliance Party allies and the PAN-PRD coalition, in a campaign that has been dominated by allegations of vote-buying on behalf of the PRI and illicit enrichment of the PAN candidate.

The PRI and the PAN-PRD were both seeking to distance themselves from Veracruz Governor Javier Duarte who has been assailed for corruption and impunity.

Since Duarte took office in 2010, the state has seen the abduction of Mexicans by state police, the assassination of journalists, and accusations of money laundering among senior government officials, including the current governor.

OPPOSITION ALLIANCES

Story continues

To have a unified front against PRI, Mexico's main opposition parties were fielding joint candidates for five governorships in Durango, Zacatecas, Oaxaca, Quintana Roo, in addition to Veracruz.

Success for the PAN-PRD alliance on June 5 could help shape strategy for 2018 if neither side reckons it can independently beat the ruling party or the center-left National Regeneration Movement (Morena), led by Andres Manuel Lopez Obrador.

Leaders of the PAN and the PRD have said they would be open to forging alliances ahead of the presidential election in 2018.

The new Morena movement has benefited from voter fatigue over established political parties, making late inroads in the Veracruz election.

The competitive race run by Morena was also strengthening the near-certain 2018 presidential bid of Lopez Obrador, who has name recognition among 96 percent of Mexicans, according to Parametria, a Mexican pollster.

Senior government officials have said in private he is the man to beat.

(Reporting by Natalie Ann Schachar and Dave Graham; Editing by Bill Rigby)