It’s idiomatic, and it usually has a negative connotation. An analogous example is criticising a school for “teaching to the exam”, usually implying that the school’s teaching is inappropriately prioritising getting the student a good grade in their exam, at the the expense of giving the student a good general education in the subject.
Here’s a contrived example of writing code to the tests, in the negative sense. Given this test:
def test_add():
assert(add(1, 1) == 2)
we could write this obviously broken implementation of the add function, which is nevertheless the simplest code that passes the test: def add(a, b):
return 2