Can humans can find a balance between treating animals ethically while also raising them for food or testing purposes?
Eating animals and animal testing are both ethically ambiguous; they depend on a personal interpretation of animal self-worth, possible sentience, and/or the responsibility of humans to not needlessly inflict pain.
It is obvious that animals have a pain response; they react instinctively to harm just as humans do, but that is not proof of sentience, simply proof that their nervous systems operate similarly to ours. A better example is how most animals can learn to avoid pain if they can help it; still, that is not proof of rational thinking, but only of rote learning, memorization of a circumstance that allows the avoidance of pain. Animal testing is often cruel and painful, but that is its nature; science cannot quantify results if the testing has no obvious affect, and animals cannot speak to explain their feelings. However, this is no excuse for deliberate mistreatment or cruelty, such as confined cages. Instead, animal testing should be performed humanely and the animals should be put down as soon as results are observed.
Personal opinion: while medical testing is ethically justifiable because of the enormous benefit to humankind, testing for cosmetics or other non-essential purpose cannot be considered ethical.
As far as eating meat, this is a part of human culture. Again, concerns over animal welfare have provided gains in the treatment of animals before slaughter, while many major operations take no such measures. The issue here is whether humane treatment of animals is important at all, considering that they will be eaten in the end. To find this balance, an individual must decide whether to ignore inhumane practice because of the eventual fate of the animal, or to make a personal decision to seek out humane farms. Either way, meat eating is not likely to stop anytime soon, and so the informed individual must make their own decision. Again, raising animals for meat does not require deliberate cruelty, so the source of the meat is more important than the ethics of eating meat itself.
Personal opinion: eating meat is by definition killing, and so justifying for any reason other than enjoyment is less valid. Instead, the decision should rest on the personal expense and effort taken in finding either meat alternatives or humanely-raised meat.