Use category for unique setup/text fixture setup

Asked by Kelly Bushue

I have at two categories for my tests. One with hardware in the loop and one with out.

I tried using Category attribute to attempt to run a setup method named TestInit() when the selected category is hardware but when the category is no hardware I want to have Nunit call a setup method named TestInitNoHardware() instead. Each Test has the appropriate category attribute. So I select just one test for example that is hardware and the category selected in the GUI is hardware... it looks like both setup methods are called regardless of the category.

Is there another way other than a completely seperate DLL (one for hardware and one for no-hardware)
Or can I programmatically check which "active" category has been selected from the GUI via code in the setup methods or in Setup method is there a way to know for the current test about to be run what it's Category is set to?

Thanks!

Question information

Language:
English Edit question
Status:
Solved
For:
NUnit V2 Edit question
Assignee:
No assignee Edit question
Solved by:
Charlie Poole
Solved:
Last query:
Last reply:
Revision history for this message
Best Charlie Poole (charlie.poole) said :
#1

No, there is no way to categorize setup methods. Categories only apply to test methods themselves and there is no way for a test method to know why (under which category) it is running. A feature similar to what you are asking for has been requested and is in our NUnit 3.0 backlog. You might take a look at the NUnit 3.0 requests and add a comment in support of it or make suggestions for changes. (Note that NUnit 3.0 is a separate project on Launchpad: nunit-3.0) You would also be welcome to engage in discussion of this feature on the nunit-discuss list on google.

For a short term workaround, the only solution I know is to use a single setup method, which detects whether you are running with or without hardware and performs initialization accordingly. You could use an environment variable to modify it's behavior.

Revision history for this message
Kelly Bushue (kelly-r-bushue) said :
#2

Fair enough. thanks for the quick reply!

Revision history for this message
Kelly Bushue (kelly-r-bushue) said :
#3

Thanks Charlie Poole, that solved my question.