The Bible teaches how to recognize the spirits. It says that every spirit who denies Jesus Christ isn't from God. But what about Acts 16:16-18? The spirit in this girl wasn't from God, and it still said that Paul was saying the truth about salvation.
"Beloved, do not believe every spirit, but test the spirits, whether they are of God; because many false prophets have gone out into the world. By this you know the Spirit of God: Every spirit that confesses that Jesus Christ has come in the flesh is of God, and every spirit that does not confess that Jesus Christ has come in the flesh is not of God. And this is the spirit of the Antichrist, which you have heard was coming, and is now already in the world" (I John 4:1-3).
In this passage, "spirit" is referring to people. We know this because he is talking about being on guard against false prophets. The spirit of a person is normally hidden from scrutiny. I can't read another person's mind and know what they are thinking. "For what man knows the things of a man except the spirit of the man which is in him?" (I Corinthians 2:11).
So how do I know whether a person coming claiming to teach the words of God is truly from God? John tells us to put people to the test. One test is to see if they admit that Jesus physically came to this world. At the time John wrote this letter there was a movement rising that we now call Gnosticism. Gnostic means hidden knowledge. Gnostics taught there were secrets that God only revealed to select individuals who were worthy of receiving it. One of their core beliefs was that anything of this world was sinful. Since Jesus was without sin, they reasoned that Jesus couldn't have really come in the flesh because that would make him sinful. Therefore, they argued that Jesus only appeared to be here in this world, but he wasn't really here physically.
What John is saying is that someone who teaches such foolishness is clearly not from God. It directly contradicts what John wrote in another book: "And the Word became flesh and dwelt among us, and we beheld His glory, the glory as of the only begotten of the Father, full of grace and truth" (John 1:14). Notice that this wasn't an acknowledgment or denial of Jesus being the Christ or of his existence. Satan and his angels have always accepted that God exists. "You believe that there is one God. You do well. Even the demons believe--and tremble!" (James 2:19).
The test is simply another application of what God taught the Israelites: "If there arises among you a prophet or a dreamer of dreams, and he gives you a sign or a wonder, and the sign or the wonder comes to pass, of which he spoke to you, saying, 'Let us go after other gods' - which you have not known - 'and let us serve them,' you shall not listen to the words of that prophet or that dreamer of dreams, for the LORD your God is testing you to know whether you love the LORD your God with all your heart and with all your soul. You shall walk after the LORD your God and fear Him, and keep His commandments and obey His voice, and you shall serve Him and hold fast to Him. But that prophet or that dreamer of dreams shall be put to death, because he has spoken in order to turn you away from the LORD your God, who brought you out of the land of Egypt and redeemed you from the house of bondage, to entice you from the way in which the LORD your God commanded you to walk. So you shall put away the evil from your midst" (Deuteronomy 13:1-5). A true prophet of God teaches things consistent with what other past prophets of God have taught.
This doesn't mean this is the only test. There are other tests. "Beware of false prophets, who come to you in sheep's clothing, but inwardly they are ravenous wolves. You will know them by their fruits. Do men gather grapes from thornbushes or figs from thistles? Even so, every good tree bears good fruit, but a bad tree bears bad fruit. A good tree cannot bear bad fruit, nor can a bad tree bear good fruit. Every tree that does not bear good fruit is cut down and thrown into the fire. Therefore by their fruits you will know them" (Matthew 7:15-20). And, "But the prophet who presumes to speak a word in My name, which I have not commanded him to speak, or who speaks in the name of other gods, that prophet shall die. And if you say in your heart, 'How shall we know the word which the LORD has not spoken?' - when a prophet speaks in the name of the LORD, if the thing does not happen or come to pass, that is the thing which the LORD has not spoken; the prophet has spoken it presumptuously; you shall not be afraid of him" (Deuteronomy 18:20-22).
"Now it happened, as we went to prayer, that a certain slave girl possessed with a spirit of divination met us, who brought her masters much profit by fortune-telling. This girl followed Paul and us, and cried out, saying, "These men are the servants of the Most High God, who proclaim to us the way of salvation." And this she did for many days. But Paul, greatly annoyed, turned and said to the spirit, "I command you in the name of Jesus Christ to come out of her." And he came out that very hour" (Acts 16:16-18).
The spirit in this girl was not of God because fortune-telling is contrary to the teachings of God. "There shall not be found among you anyone who makes his son or his daughter pass through the fire, or one who practices witchcraft, or a soothsayer, or one who interprets omens, or a sorcerer, or one who conjures spells, or a medium, or a spiritist, or one who calls up the dead. For all who do these things are an abomination to the LORD, and because of these abominations the LORD your God drives them out from before you" (Deuteronomy 18:10-12). Paul had no interest in the "testimony" of an evil spirit. It had no value; after all, how could you tell if the spirit was telling the truth or not? Worse by evil giving support, it made it look as if Paul was in league with the spirit. Hence, Paul cast it out.