As Daniel Appleman, has noted in his book "Advanced Apex", it is important to design triggers so to be smart with respect to recursion and re-entrancy. Take the following requirement that mirror our use case:
Ensure a new CustomObject1 has a lookup towards Account set to the Account whose Field2 (ExternalId) is equal to CustomObject1.Field1. If such an Account does not exist, create it. Such a lookup is required.
Because the lookup is required, we implementd a beforeInsert/beforeUpdate trigger using a static Set, which we would use to store the value of Field1 that we already processed so that subsequent trigger executions would not perform the same work of creating account if it doesn't exist.
However, we notice that this was leading to test failures because, in our tests, we insert two different CustomObjects having the same Field1 value with two separate DML statements, which is a single transaction.
Because your objects have no id yet, you can't use them to determine if you have already processed them. You need to a static Map, whose key is either the System.hashCode of your CustomObject or if you are lucky, an ExternalId on CustomObject. What is the most appropriate solution to this problem?