U.S. bank regulators breathed a huge sigh of relief in early April when improving financial markets looked set to push the nation's 19 largest banks through the gauntlet of tough stress tests in reasonably good shape.

In early March, a month after Treasury Secretary Timothy Geithner announced the tests on February 10 to help restore investor confidence in the major banks, the scene was much bleaker: major stock indexes had slumped to 12-year lows on persistent fears about the financial weakness. It looked possible that a major bank might need an emergency government rescue even before the stress test results could be announced.

But since the trough, markets improved steadily with rising share prices and volumes, better liquidity and other signs of stabilization, and regulators gained comfort that capital markets would be willing to fill any holes the stress tests unearthed at banks.

It looked to us by mid-March, and early April, that this might work, said a senior U.S. regulatory official, who spoke on condition of anonymity because of the sensitivity of the tests.

Stress tests released on Thursday showed the largest banks had a $74.6 billion gap to make up to ensure they could withstand a worst-case scenario regulators set for them. Nine of the 19 banks tested already had the necessary buffer and would not need to raise any more.

Morgan Stanley and Wells Fargo sold more than $15 billion of shares and bonds the next day, and Bank of America Corp announced plans to sell 1.25 billion shares as investors reacted positively to the relatively small size of the shortfall.

If by the middle of next week banks are half-way to raising the needed capital, and a half dozen or so of the banks needing to augment their buffers have done so, regulators would be pleased, the official said. In particular, such a development would likely signal the administration would not have to ask Congress to replenish its bailout war chest.

TEST IDEA TOOK SHAPE IN SEPTEMBER

The stress test idea germinated in early September as officials at the Federal Reserve and the New York Fed, including Geithner who was then president of the New York Fed, took part in conference calls to discuss deteriorating financial markets.

But the crisis intensified with the collapse of Lehman Brothers and the Fed rescue of American International Group, and the stress test idea was put on the backburner. By the time the administration of President Barack Obama took office, however, there was breathing room to try the idea.

Regulators at the Fed, the Office of the Comptroller of the Currency and the Federal Deposit Insurance Corp teamed with researchers and statisticians to design a model that would test the 19 banks at once according to the same standards, and then make the results public, an unprecedented undertaking.

As regulators refined the model, they ironed out bugs in the test and settled disputes among officials at different regulatory agencies including at regional Fed banks.

Disagreements included whether a particular bank's assets were of higher quality than average and what bank earnings were likely to be over the next two years, particularly if a bank had acquired a troubled asset, such as another financial institution that was in trouble.

Regulators shared the all-but-final version of the tests with all 19 banks on April 24 at the regional Feds that govern the districts in which the banks are headquartered. Regional Fed bank presidents participated in most sessions, but Fed board officials did not.

Early market reaction this week showed the announcement of the stress test results, which some had feared would destabilize vulnerable banks by exposing weaknesses, had stoked enthusiasm. U.S. stocks rose on Friday, led by financial shares.

The release of the stress test results has given people a little bit of confidence that the government can help to solve this part of the financial crisis, said Richard Sparks, senior equities analyst and options trader at Schaeffer's Investment Research in Cincinnati.

There's a sense that the government actually has a logical plan ... even if things got worse, these companies will be able to survive, Sparks said.

(Additional reporting by Ellis Mnyandu and Chuck Mikolajczak in New York; editing by Mohammad Zargham)