Declare preprocesor symbol (like DEBUG) globally f

2020-02-11 04:49发布

问题:

I would like to switch between NUnit and VS Tests like this:

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
  using NUnit.Framework;
  using TestClass = NUnit.Framework.TestFixtureAttribute;
  using TestMethod = NUnit.Framework.TestAttribute;
  using TestInitialize = NUnit.Framework.SetUpAttribute;
  using TestCleanup = NUnit.Framework.TearDownAttribute;
  using TestContext = System.String;
  using DeploymentItem = NUnit.Framework.DescriptionAttribute;
#endif

My question is, how may I declare NUNIT preprocesor symbol at one place (App.config or so, would be great), to switch between NUnit and VSTests easily? Because when I use #define NUNIT, it works only for the file, where it is written.

回答1:

Use the project properties dialog. You can define global symbols there:

Right click on the project -> Properties -> Build tab -> Conditional compilation symbols



回答2:

The only way to do this per project is via the project / build itself (project properties -> build -> conditional compilation symbols). You can define multiple "configurations" for a project (with different symbols defined), and use the one you want.

To add a new configuration, use the configuration manager (at the bottom of the debug/release drop-down) and create a new one based on on of the existing (debug/release/etc). Now in the project properties you can choose this option to set the different symbols for that config.