While it may seem contradictory, it is possible to kill animals for food and to use them for scientific testing while still treating them ethically. We can think of this as being similar to how we have laws of war. War is inherently terrible, and yet we have laws to try to make it as humane as possible. The same can be true of how we treat animals.
There are two prongs to how we should treat animals. First, we should only kill or harm them when it is really necessary. That is, we should be able to kill them when we need to eat them. We should be able to harm or kill them in order to pursue scientific breakthroughs that will save lives. However, we should not harm or kill them for frivolous reasons. We should not do this just for fun or because we want to test cosmetics on them.
A second prong should be that we should treat the animals as humanely as possible while they are alive and should kill them as painlessly as possible. Of course, it is hard to draw the line between humane treatment and needlessly humane treatment. In other words, it is hard to know whether we need to give chickens larger cages or whether we need to allow them to be “free-range.” However, we should not subject them to pain on an everyday basis and when we kill them it should be swift and painless if at all possible.