BALTIMORE, JULY 17 -- In what may become a national test case, a Baltimore judge today upheld a city ordinance requiring the city's $1.2 billion employes' pension fund to sell its interests in corporations doing business in racially segregated South Africa.

Bringing to a head arguments that have raged between antiapartheid activists and pension fund administrators in more than 20 states and 70 cities throughout the country -- including Washington -- Circuit Judge Martin B. Greenfeld found little or no conflict between the objectives of city political leaders to force U.S. corporate giants out of South Africa through divestiture and the obligation of pension trustees to get the best investment yield for retired employes.

The case is believed to be the nation's first test of city laws that force the divestiture of local pension funds in companies doing business with South Africa, and is considered a landmark by many legal scholars and civil rights advocates.

Many pension fund trustees around the country have argued that companies investing in South Africa provide the greatest yield for them and that the retirees they serve could lose money if they had to pull out of those investments.

But Greenfeld ruled that although divestiture and subsequent reinvestment of pension funds in a smaller range of companies may cause some initial extra costs and possible losses, the amounts are so small that they do not violate the Baltimore trustees' legal obligation to seek the best investment yield or impair their contractual relationship with the pensioners.

"Even if the impairment were more significant," Greenfeld said, "it would be insubstantial when compared to the salutary moral principle which generated the ordinance" in the first place.

In a 32-page opinion, the judge also swept aside broad constitutional arguments that the divestiture ordinance intrudes on the federal government's prerogative to conduct foreign affairs and violates the foreign and interstate commerce clauses of the Constitution.

Attorneys for the 19-member Baltimore City Council that passed the divestiture ordinance last year expressed elation at Greenfeld's ruling.

"He hit all the points that we argued," said attorney H. Russell Frisby Jr. "We are very pleased with the decision."

Frisby, noting concern by municipal pension fund administrators across the country that divestiture could trigger serious financial losses, said "I hope that {Greenfeld's} opinion helps put that fear to rest."

Attorneys for Baltimore's pension fund expressed disappointment and said an appeal is likely.

The case has drawn national attention, especially among lawyers representing municipal employe pension funds, and is thought to be one of the first to be tested in court on constitutional grounds. Although the decision is not binding on any other such cases, it can act as a guide to lawyers considering similar litigation.

"It's a national test case, definitely," said Frisby.

Several civil liberties and antiapartheid groups also have followed the case, and the Reagan administration has taken an interest in it as well. During a recent 10-day trial before Greenfeld, observers from the Treasury and State departments were in the courtroom, according to lawyers in the case.

Baltimore's divestiture ordinance, modeled on a similar measure in the District of Columbia, requires the pension fund in the next two years to sell all interests it has in corporations doing business in South Africa and neighboring Namibia.

Attorneys say that the fund, responsible for paying pension checks to about 8,000 retired city workers, will have to sell about 40 percent of its investments to comply with the law.

While the attorneys argued that divestiture would impose additional costs and require the pension fund to enter the less stable investment field of smaller companies that are not as likely to do business in South Africa, Greenfeld countered that the divestment costs would be only a fraction of 1 percent of the total pension fund.

Also, he said, "investment in smaller companies with the attendant greater volatility does not necessarily result in a significantly impaired risk," and "if prudently selected, will do at least as well as, if not better than, stocks in the larger companies which must be divested."

On the constitutional question of whether the divestiture ordinance intrudes on the exclusive foreign policy authority of the federal government, Greenfeld cited Supreme Court cases holding that such local laws can be allowed as long as they have only an "incidental or indirect effect in foreign countries."