Unit Tests

When using Babel Obfuscator, it is possible to maintain unit tests even after obfuscating the code. This is achieved by configuring Babel Obfuscator to apply obfuscation to the code and run the unit tests against the obfuscated code. This setup requires using an XML mapping file, which helps to resolve issues that may arise from the access of internal classes consumed by the unit tests. This way, you can ensure that the unit tests continue to provide reliable and accurate results even after the code has been obfuscated. This helps maintain the code's quality and ensures that it continues to function as intended after obfuscation.

To effectively run unit tests against obfuscated code, it is necessary to use an XML mapping file with Babel Obfuscator. The XML mapping file helps Babel Obfuscator keep track of the names of your classes, methods, and variables in your target assembly and ensure that they remain consistent even after obfuscation. This is important because unit tests often depend on the internal structure of your code and may use references to specific classes, methods, or variables. If these references are changed during obfuscation, the unit tests will no longer be able to access the necessary elements making the tests fail.

To overcome this issue, Babel Obfuscator generates an XML mapping file for the target assembly that records the original names of your classes, methods, and variables before obfuscation. During the obfuscation process, Babel Obfuscator updates the names in the mapping file to match the obfuscated names. The unit tests can then be processed using Babel Obfuscator and the generated mapping file to access the correct obfuscated names, allowing them to run correctly against the obfuscated code.

In order to use Babel Obfuscator with your unit tests, you need to take the following steps:

  1. Add a reference to the Babel Obfuscator NuGet package in the unit test project.

  2. Modify the project by adding MSBuild instructions allowing Babel to load the XML mapping file generated for the target assembly.

  3. It is not necessary to enable renaming for the unit test project, so it should be disabled.

  <PropertyGroup>
    <ObfuscateTypes>false</ObfuscateTypes>
    <ObfuscateProperties>false</ObfuscateProperties>
    <ObfuscateMethods>false</ObfuscateMethods>
    <ObfuscateParameters>false</ObfuscateParameters>
    <ObfuscateFields>false</ObfuscateFields>
    <ObfuscateEvents>false</ObfuscateEvents>    
  </PropertyGroup>
  <ItemGroup>
    <MapInFile Include="$(SolutionDir)/MapOut/ClassLibrary.dll.map.xml" />
  </ItemGroup>

Where ClassLibrary.dll.map.xml is the XML map file generated for the target assembly we are testing.

Doing this allows you to run your unit tests against the obfuscated code and use the XML mapping file to resolve any issues with references to obfuscated names consumed by the unit test code. This setup will ensure that your unit tests continue to work even after obfuscation is applied to your code.

You can download the sample project here. To run the sample project, please follow the instructions below:

  1. Extract the UnitTest.zip file into a folder on your computer.

  2. Copy your license file to the extracted folder.

  3. Open a command prompt or terminal window and navigate to the folder where you extracted the files.

  4. Enter the command "dotnet test" to run the sample project.

Last updated