The commandment against putting God to the test comes from Deuteronomy 6:16, as part of a series of instructions relating to loyalty to the one true God:
Do not put the Lord your God to the test as you tested him at Massah.
The reference to Massah is from the events of Exodus 17:1-7, also recounted in Numbers 20:1-13. This is the story where the Israelites have come out of Egypt, have been wandering in the desert, and are getting fed up with the harsh conditions and lack of water - they have lost their trust in God's promise. Moses, at God's command, strikes the rock with his staff to let out a stream of water. Exodus 7:7 explains: "He gave the place the names Massah and Meribah because of the Israelites' contentiousness and because they put the Lord to the test by saying, 'Is the Lord with us or not?'". 'Massah' means 'trial' and 'Meribah' means 'contention'.
The account in Numbers is a little more colourful.
There was no water for the community, so they banded together against Moses and Aaron. The people laid the blame on Moses. "We would rather have died," they said, "as our brothers died before the Lord! Why have you brought the Lord's community into this desert, for us and our livestock to die here? Why did you lead us out of Egypt, only to bring us to this wretched place? It is a place unfit sowing, it has no figs, no pomegranates, and there is not even water to drink!" (Nb 20:2-5)
The Lord then said to Moses and Aaron, "Because you did not believe that I could assert my holiness before the Israelites' eyes, you will not lead this assembly into the country which I am giving them." These were the Waters of Meribah, where the Israelites laid the blame on the Lord and where, by their means, he asserted his holiness. (Nb 20:12-13)
This context shows that 'putting God to the test' is strongly connected to one of the major themes of the exodus - that despite God's continual promises, people are still doubtful, resentful, unsatisfied, and disobedient. We demand more and more evidence of God's love for us (sweetening the foul water, raining manna, making water spring from the rock, ...) rather than accepting and returning it. This is what is going on in the passage from Luke - the devil is suggesting that God's unconditional love is a resource to be exploited.
The statement of Ahaz in Isaiah 7:12 should be seen in the context of his reign, which is described in 2 Kings 16.
[Ahaz] did not do what the Lord his God regards as right, as his ancestor David had done. He followed the example of the kings of Israel, even causing his son to pass through the fire of sacrifice, also copying the disgusting practices of the nations whom the Lord had dispossessed for the Israelites. (2 Kings 16:2-3)
Ahaz was no true follower of God. As a king, besieged by Razon and Pekah (2 Kings 16:5 and Isaiah 7:1), his response was to offer his kingdom as a vassal state to Assyria. He sent treasures from the Temple to Assyria's king (2 Kings 16:8), and made other changes to the Temple structure (16:17-18).
The New Jerusalem Bible has this to say in a footnote to Isaiah 7:7-9 :
For the prophets, faith is not so much a theoretical belief in the existence and uniqueness of God as an attitude of confidence based on God's choice of Israel: he has chosen Israel, he is Israel's God, he alone has the power to save his people. This unconditional trust, a guarantee of salvation, excludes all reliance not only on human beings but still more on false gods.
So when Ahaz says, "I will not put God to the test," he is showing a lack of faith in God. He would rather trust in the mighty Assyrian empire, or in foreign gods, than accept the sign that God has just promised to give him. God demonstrates his patient love yet again by giving the sign anyway - a young woman (or virgin) is "with child and will give birth to a son whom she will call Immanuel" (Isaiah 7:14).