The city has doled out more than $1.5 million in legal fees over the past decade defending a controversial hair test for cocaine use that critics claim discriminates against black officers.

But cops say they stand by the test as a federal case hangs in the balance.

The city has spent about $1.57 million since September 2006 fighting legal challenges in both state and federal court defending Boston police’s use of the hair test, according to documents obtained by the Herald after a civil rights group filed a records request.

Since 2012, two district court judges ruled in favor of the city’s hair test, while in 2014, a decision in the first circuit ruled in favor of the suing cops.

Plaintiffs argue that black officers have higher levels of melanin in their hair, which causes cocaine to bind to hair at higher rates. That leads to false positives, they say.

Earlier this month, the state’s highest court let a ruling stand that found the department wrongly fired six officers who tested positive for drugs, five of whom are minorities. The ruling required the department to provide the officers back pay and benefits retroactive to 2010.

Those officers have won their jobs back. But state court rulings have upheld the termination of four other officers who failed the test.

The city’s high legal bill did not sit well with minority advocates.

“I’m outraged — it’s unconscionable … It’s not the city’s money, it’s the taxpayers’ money. And the taxpayers should be outraged,” said Larry Ellison, executive director of the Massachusetts Association of Minority Law Enforcement Officers, which is a plaintiff in a separate federal case. Ellison added, “Diversity is something they talk about, but it’s not something they want. It’s not something they embrace.”

But Boston police said no courts have invalidated the test and drug use in the police department is something Commissioner William B. Evans won’t tolerate.

“There is no proof that says it’s not valid,” said BPD spokesman Lt. Det. Michael McCarthy. “We continue to use it now and there are no plans to change that. … The commissioner has an obligation to ensure that the residents of the city are getting officers who are fit in performing duties.”

McCarthy said he can’t speak about the ongoing federal case, which is in the U.S. Court of Appeals First Circuit.

Ivan Espinoza-Madrigal, executive director of the Lawyers’ Committee for Civil Rights and Economic Justice, which is litigating the federal case on behalf of 10 black officers and Ellison’s minority police association, said: “It’s irresponsible for the city of Boston to invest limited taxpayer resources in defending discriminatory programs that stand in the way of diversifying the Boston Police Department.”