TDD, Flask & SQLAlchemy

For a personal project I’m currently working on, I’m using test-driven development. For those not familiar with test-driven development (or TDD), the basic idea is writing tests that fail first. The next step is writing the minimal amount of code to pass the test. And finally refactoring, cleaning, and improving the code while using the tests to ensure nothing breaks. After which you repeat the process. (see the Wikipedia article)

While working on a SQLAlchemy model which included a foreign key (reference) to another table in the database, I was writing a test for adding this reference. To my suprise, when I ran the test, it passed. Turns out, because the attribute did not yet exist in the SQLAlchemy model, Python created it as a normal attribute when trying to add the reference (quite logical in hindsight). So when seeing if the value of said field was correct, it would return True, but this would not come from the database, but from the object itself.

For now I wrote a defective version of the attribute first, to check if the tests fails as expected. Then following the TDD steps and writing the minimal amount of code to pass. This is not a very clean solution in my opinion, but it is the best I could find at the moment and I would gladly receive any other suggestions on writing tests in this specific case.