Teaching to the test is condemned as poor instruction because it narrows the focus of education. That's not the fault of teachers. It's the fault of those who put too much reliance on tests.
If a test is created, one expects that those who are being evaluated by it will do what they can to ensure a positive evaluation. That's the point of standards; that's the point of tests.
Furthermore, a test is often intended to define a standard that one wants to impose of those things that are being measured. For example, the octane rating of gasoline (whatever good that is; and I don't know) defines a standard that the manufacturers of gasoline are required to meet when they label their product as being of a certain octane. That's manufacturing to the test. There is nothing wrong with that. Similarly household devices that run on the current delivered to your wall by electric utilities are manufactured "to the test" of the electric standard for the country in which they are sold. They are not allowed to be labelled, e.g., 110V, 60 Hz, unless they meet the test of operating under those conditions.
Tests and standards are self-defeating when they don't really measure what they are intended to measure. But if we don't over-estimate the significance of meeting certain standards, the existence of the standards can be helpful—even when the standards don't automatically achieve the goals they were intended to achieve.
For example, when food manufacturers modified the ingredients they used because they knew the food labelling law required that the package show the amounts of fat and and they wanted to be able to claim that they were selling a low-fat product, that was a good thing. Yet they were modifying their behavior to achieve something that was available because a test was created in the environment. They were, in effect, teaching to the test. In this case, it wasn't even a test, just a requirement that the contents be a bit more transparent according to some objective standard. Of course, creating a lower fat product by raising the sugar content (as many manufacturers did) wasn't particularly healthy. So reducing the fat content didn't automatically make the food healthier. But at least the intelligent buyer has more information about what is in the package.