Unity 6 asset bundle build issue with test framework

I’m updating an existing project to Unity 6 (from 2021), and have run into an issue with play mode tests in an Android build.

We have our own build scripts, and when making an automation build we have a scripting define for that. The define is tested for in the build script, and if set it adds the “IncludeTestAssemblies” flag in the BuildOptions passed to the eventual BuildPlayer call. Our define also wraps all test code. This is working fine for the player build in both 2021 and Unity 6.

Before we build the player though, as part of the pipeline we build asset bundles. In 2021 there are no issues, but in Unity 6 we’re getting errors of the form “type or namespace name could not be found” for the Unity.TestTools-based attributes, eg. UnityTest, when the bundles are built with our automation flag active.

I think what MIGHT be happening is that IncludeTestAssemblies isn’t being set for the bundle build, but I see no way to pass BuildOptions via BuildAssetBundles. Is this likely to be the problem? If so is there a known fix? And if not, what else might be happening?