Search code examples
sql-serverpostgresqlnunitintegration-testingnunit-3.0

How to run NUnit onetimesetup multiple times for different databases


We have an integration test suite that contains numerous tests executing repository classes.

The objective is to have a [OneTimeSetup] method in the BaseTestFixture, that will create/populate each target database (Postgres/SQL Server) only once before all tests and teardown after all tests.

Got this error:

nunit OneTimeSetUp: SetUp and TearDown methods must not have parameters

How can we run the entire test suite against Postgres, SQL server and both without duplicating tests?

Thanks.


Solution

  • Interesting question. I can't really think of an 'out-the-box' solution myself

    One simple workaround would be to do two separate console runs and use the --params flag. That way, you could run a different setup for each database type, dependent on the TestParameters value passed in.

    A nicer alternative may be to implement a custom attribute, which would allow you to parameterize SetUpFixtures. (There's an existing discussion on adding this feature here- although it hasn't garnered much interest since 2016.) I think it would be reasonably possible to do this as a custom attribute without modifying NUnit however.

    Take a look at how SetUpFixtureAttribute is implemented. I would think you'd want to create your own IFixtureBuilder attribute which works in a similar way, except can be parameterised, and return two suites, with a different setup for each database. I think that would work, although it's not functionality I'm totally familiar with myself.