How to set the default theme programmatically
The current advice is to set this via config:

\Drupal::configFactory()
  -&gt;getEditable('system.theme')
  -&gt;set('default', $theme)
  -&gt;save();


The previous method using ThemeHandlerInterface::setDefault() is now deprecated.

You might need to install it first too, I'm not sure. In case you do:

\Drupal::service('theme_installer')-&gt;install(['test_theme']);