← Back to team overview

nunit-core team mailing list archive

Re: [Question #141938]: Test a Suite of Fixture

 

Question #141938 on NUnit Framework changed:
https://answers.launchpad.net/nunit-3.0/+question/141938

    Status: Needs information => Solved

Maxence MODELIN confirmed that the question is solved:
Hey Charlie,

I think that I found my solution. 
As u suggested me, I removed the FixtureAttribute. 
It seems (but maybe I'm wrong), that when a Fixture contained a TestCaseSource, the instantiation is done 2 times.
For reminder, I was trying to make a Suite of Fixtures instantiated with a parameter and each were containing a TestCaseSource based on a property depending of the parameter given.
So finally i deduced : 
- the instantiation of the Fixture (done by the Suite) is not the same than the instantiation of the TestCaseSource property : instantiation of a same fixture was done 2 times : once by the Suite and then a second time to generate the TestCaseSource list of tests
Which means that it's not possible to give a parameter to a fixture containing a TestCaseSource because this fixture will be reinstantiated one more time after with the basic constructor (so the parameter will be missing).

I solved my solution with a weird ParameterProvider singleton class containing a queue filled in the Suite creation. 
I join my code (maybe that could help someone - i'm not very proud of it but it seems to solve my issue)

Thanks again for your help Charlie and your quick answers!!!
max

public class ParameterProvider
{
	#region Singleton region
	/// <summary>
	/// Private constructor for singleton
	/// </summary>
	private ParameterProvider()
	{
	}
	private static ParameterProvider instance;

	public static ParameterProvider Instance
	{
		get
		{
			if (instance == null)
			{
				instance = new ParameterProvider();
			}
			return instance;
		}
	}
	#endregion

        private Queue<string> _fixturesQueue = new Queue<string>();

	/// <summary>
	/// Provide the first item on the queue, remove it and reinject it at the end of the queue
	/// </summary>
	/// <returns></returns>
	private string Dequeue()
	{
		var item = _fixturesQueue.Dequeue();
		_fixturesQueue.Enqueue(item);
		return item;
	}

	private void Enqueue(string item)
	{
		_fixturesQueue.Enqueue(item);
	}

	/// <summary>
	/// Contains the amount of item contained in the queue
	/// </summary>
	private int Count
	{
		get
		{
			return _fixturesQueue.Count;
		}
	}

	static public void AddOne(string item)
	{
		ParameterProvider.Instance.Enqueue(item);
	}

	static public string GetOne()
	{
		if (ParameterProvider.Instance.Count == 0)
		{
			return null;
		}
		return ParameterProvider.Instance.Dequeue();
	}
}


/// <summary>
/// This property contains the fixture list to execute
/// </summary>
[Suite]
public static IEnumerable Suite
{
	get
	{
		var suite = new ArrayList();
		var files = Directory.GetFiles(ConfigurationPath.GetFolderPath());
		foreach (var file in files)
		{
			FileInfo fileInfo = new FileInfo(file);
			ParameterProvider.AddOne(name);
			suite.Add(new ParametrizedFixture());
		}
		
		return suite;
	}
}

And the "parametrized" fixture

[TestFixture]
public class ParametrizedFixture
{
	public ParametrizedFixture()
	{
		InitializeFixture();
	}

	private static IList<string> _functionsToTestList;
	private readonly string _testFilePath = ParameterProvider.GetOne();

	#region Properties
	/// <summary>
	/// Provide the function list to test inside this fixture
	/// </summary>
	/// <returns></returns>
	public IEnumerable<string> FunctionsToTest()
	{
		if (_functionsToTestList == null)
		{
			throw new NullReferenceException("Functions list is null");
		}
		return _functionsToTestList;
	}
	#endregion


	private void InitializeFixture()
	{
		// Get function to test in this fixture
		_functionsToTestList = GetFunctions(_testFilePath);
	}

	static private IList<string> GetFunctions(string file)
	{
		// Find the test to execute. For instance :
		var testsToBeDone = new List<string>();
		for (int i=0; i < 3; i++ )
		{
			testsToBeDone.Add("Test" + i);
		}
		// But in my project : this list was depending of the property _testFilePath
		return testsToBeDone;
	}

	[Test]
	[TestCaseSource("FunctionsToTest")]
	public void PowerShell_Tests(string functionToTest)
	{
		// Tests to execute
	}
}

-- 
You received this question notification because you are a member of
NUnit Core Developers, which is an answer contact for NUnit Framework.