Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetUpFixture not being run if there are no tests #3420

Open
akorczynskikcura opened this issue Nov 5, 2019 · 5 comments
Open

SetUpFixture not being run if there are no tests #3420

akorczynskikcura opened this issue Nov 5, 2019 · 5 comments

Comments

@akorczynskikcura
Copy link

akorczynskikcura commented Nov 5, 2019

We have a test assembly with just SetUpFixture and no tests. The reason being is that we have multiple test assemblies and want to run setup code before any test executes, so we just run nunit on this special assembly first.

	namespace myNamespace.SetUpFixture
	{
		[SetUpFixture]
		public class SetUpFixture
		{
			[OneTimeSetUp]
			public void SetupTests()
			{
				Console.WriteLine("SetUpFixture");
			}
		}
	}

As a workaround I have to add fake test fixture class and fake test.

With this issue #1713 I have read that the agreement was that setups and teardowns are still run even if there are no tests. Is this still true?
Documentation however does not mention anything about it https://github.com/nunit/docs/wiki/SetUpFixture-Attribute.

There is also another issue with SetUpFixture #3356, but it is for newer version and it has a test inside.

I have tested Nunit-console 3.9.0 and 3.10.0. They all succeed, meaning zero tests were found, but SetUpFixture was not executed.

10:51:22.787 Debug [ 5] DefaultTestAssemblyBuilder: Loading S:\myNamespace.SetUpFixture.dll in AppDomain domain-
10:51:22.794 Debug [ 5] DefaultTestAssemblyBuilder: Examining assembly for test fixtures
10:51:22.795 Debug [ 5] DefaultTestAssemblyBuilder: Found 1 classes to examine
10:51:22.810 Debug [ 5] DefaultTestAssemblyBuilder: Found 1 fixtures with 0 test cases
10:51:22.989 Info  [ 5] DefaultTestAssemblyRunner: Running tests
10:51:22.995 Info  [ 5] Dispatcher: Initializing with 12 workers
10:51:22.998 Debug [ 5] WorkShift: Parallel shift adding queue ParallelQueue
10:51:22.998 Debug [ 5] WorkShift: Parallel shift adding queue ParallelSTAQueue
10:51:22.998 Debug [ 5] WorkShift: NonParallel shift adding queue NonParallelQueue
10:51:22.998 Debug [ 5] WorkShift: NonParallelSTA shift adding queue NonParallelSTAQueue
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#1
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#2
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#3
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#4
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#5
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#6
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#7
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#8
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#9
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#10
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#11
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelWorker#12
10:51:22.999 Debug [ 5] WorkShift: Parallel shift assigned worker ParallelSTAWorker
10:51:22.999 Debug [ 5] WorkShift: NonParallel shift assigned worker NonParallelWorker
10:51:22.999 Debug [ 5] WorkShift: NonParallelSTA shift assigned worker NonParallelSTAWorker
10:51:23.009 Debug [ 5] Dispatcher: Using NonParallel strategy for myNamespace.SetUpFixture.dll
10:51:23.011 Info  [ 5] WorkShift: NonParallel shift starting
10:51:23.013 Info  [ 5] TestWorker: NonParallelWorker starting on thread [9]
10:51:23.013 Info  [ 5] WorkItemQueue: NonParallelQueue.0 starting
10:51:23.014 Info  [ 9] TestWorker: NonParallelWorker executing myNamespace.SetUpFixture.dll
10:51:23.041 Debug [ 9] Dispatcher: Using NonParallel strategy for [default namespace]
10:51:23.041 Info  [ 9] TestWorker: NonParallelWorker executing [default namespace]
10:51:23.042 Info  [ 9] Dispatcher: Saving Queue State for [default namespace]
10:51:23.044 Debug [ 9] WorkItemQueue: NonParallelQueue.0 pausing
10:51:23.044 Info  [ 9] WorkItemQueue: NonParallelQueue.1 starting
10:51:23.050 Debug [ 9] Dispatcher: Using Direct strategy for myNamespace.SetUpFixture.dll OneTimeTearDown
10:51:23.057 Info  [ 9] WorkShift: NonParallel shift ending
10:51:23.057 Debug [ 9] WorkItemQueue: NonParallelQueue.1 pausing
10:51:23.059 Info  [ 9] Dispatcher: Restoring Queue State
10:51:23.060 Info  [ 9] WorkItemQueue: ParallelQueue.0 stopping - 0 WorkItems processed
10:51:23.060 Info  [ 9] WorkItemQueue: ParallelSTAQueue.0 stopping - 0 WorkItems processed
10:51:23.060 Info  [ 9] WorkItemQueue: NonParallelQueue.0 stopping - 2 WorkItems processed
10:51:23.060 Info  [ 9] WorkItemQueue: NonParallelSTAQueue.0 stopping - 0 WorkItems processed
10:51:23.060 Info  [ 9] TestWorker: NonParallelWorker stopping - 2 WorkItems processed.
Test Files
    S:\myNamespace.SetUpFixture.dll


Run Settings
    DisposeRunners: True
    InternalTraceLevel: Verbose
    WorkDirectory: S:\
    ImageRuntimeVersion: 4.0.30319
    ImageTargetFrameworkName: .NETFramework,Version=v4.6.2
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 12

Test Run Summary
  Overall result: Passed
  Test Count: 0, Passed: 0, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0
  Start time: 2019-11-05 09:45:47Z
    End time: 2019-11-05 09:45:48Z
    Duration: 0.806 seconds

If I tried Nunit 3.12 and Nunitlite-runner it fails not finding test fixtures

1) Invalid : S:/myNamespace.SetUpFixture.dll
No test fixtures were found.

Please advise whether this is a bug, what is the expected behavior and what are your recommendations for this scenario. Thank you!

@lobster2012-user
Copy link
Contributor

lobster2012-user commented Nov 5, 2019

Hi, @akorczynskikcura !
1.

Not sure if you can see the result of output to the console, if only through Debug.WriteLine
(in Global SetUp\Teardown methods )

  1. I do not quite understand what exactly you do.
    I created a base library nunit_tests_fixture_base and a base (abstract) class SetUpFixtureBase
    Then in the library with tests, I created a class without a namespace
    AssemblySetUpFixture : SetUpFixtureBase
    When you run tests, global methods are launched.

@CharliePoole
Copy link
Contributor

There is no SetUp or TearDown in NUnit, which operates across assemblies. That's because the NUnit framework is invoked separately for each assembly. Such a global SetUp has been requested before, but it would have to operate outside of the framework itself, most likely within the TestEngine.

Your workaround in this case is to simply run the code, which performs your global SetUp separately. There's no need for it to be an NUnit test. Note that any SetUp you perform can only usefully impact the overall environment. For example, you could set up a database or create some file. You would not be able to directly change any properties used within the actual tests.

Note that any environmental setup you perform has to be accessible to the tests. In order to run multiple test assemblies in parallel, any resources need to be shareable.

@akorczynskikcura
Copy link
Author

@lobster2012-user you would need to have a new library without any tests, just [SetUpFixture] like in my example.

@akorczynskikcura
Copy link
Author

@CharliePoole true, not having a global setup, I can do it in a variety of ways, like create a separate executable or write a regular test, not setup.
This is submitted as a defect, since NUnit is behaving not as expected, at least according to what I have read. So, should NUnit execute setup fixture if there are no tests or not?

@CharliePoole
Copy link
Contributor

@akorczynskikcura That is the same question raised in #1713 WRT SetUpFixtures. The issue remains open and the question unanswered. My own opinion is that they should not be executed unless tests under them are to be executed, but it's not my decision to make.

The comment you cite in #1713, relates to a prior decision about a selected TestFixture containing no tests, which is a different matter. SetUpFixtures of course never contain tests.

IMO, both issues should be resolved by updating the docs to describe the current behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants