Intro

Visual Effects Pipelines often consist of legacy code. The code was written a long time ago and is now hard to read and hard to work with. Changing legacy code is complicated. Since it’s hard to read, it’s also hard to change without accidentally breaking something. Automated tests can help to prevent bugs during development. Michael C. Feathers presented an excellent method in “Effectively working with legacy code”. The method is called “Edit and Cover”. The legacy code is at first covered with tests and can be changed safely afterwards.

But it’s not easy to write tests for legacy code. Legacy Code was often not written to be executed in automated tests. Components or classes are hard to instantiate, cause side effects during construction, or have hard dependencies on external resources such as databases.

The goal

The goal of this example is to take some legacy code and write tests for that. First, the code needs to be able to run inside a test function. Then the code can be covered with more tests until it’s pretty sure that breaking the code will be caught by the tests. Finally, the code will can be refactored or changed.

The code

The legacy code is the original code from the pipeline of a Visual Effects Company. The company name is anonymized. The code works correctly and runs succesfully in production for several years. It’s one single function that deals with the creation of new work files in Maya. Whenever an artist creates a new file inside Maya, this code is run. It takes care of saving the file, registering the file in the database, and setting up some Maya settings. The function’s name is create_maya_scene.

This example was chosen because many TDs are familiar with writing code like that.
Furthermore, it includes two other challenges related to testing, which are typical for a Visual Effects Pipeline. These are interactions with DCCs and databases. DCC interactions are challenging, because usually, the code needs to run inside a DCC. Databases are challenging for tests, because they must be available during tests, might need to contain some test data, and get modified during test execution when data is saved.

The code is written in Python. SQLAlchemy is used as an ORM framework to access the database. The tests will be written using the pytest framework.

Last modified August 24, 2020