MADRID — Spain’s government was on the brink of collapse Thursday as opponents of Prime Minister Mariano Rajoy appeared to have mustered enough support to oust him in a parliamentary no-confidence vote stemming from a corruption scandal.

The choice for Mr. Rajoy was increasingly stark: jump first and resign his post, or wait to be pushed out on Friday when the Parliament was scheduled for a vote he seemed nearly certain to lose. Either outcome would mean a change of leadership for Spain and would send the country sooner or later to new elections.

The sudden turn in fortune for Mr. Rajoy, one of Europe’s longest serving prime ministers, is certain to be the next test of the stability of southern Europe, after financial markets were sent reeling by a bumpy political week in Italy, where an anti-Europe, populist government was poised to take power after an 88-day postelection impasse.

But unlike Italy, where the country’s economic stagnation under the euro has become a central issue, the issue in Spain is Mr. Rajoy himself and a long-building corruption scandal that has tainted his conservative Popular Party.