How to make feature.cs file automatically add custom NUnit category attribute from app.config

243 views
Skip to first unread message

Triet Nguyen

unread,
Jul 12, 2016, 8:52:56 AM7/12/16
to SpecFlow
From app.config:

<batches>
    <batch1>
     <something>
    </batch1>
<batch2>
     <something>
    </batch2>
  </batches>

How could I make the generated feature.cs file adding the category as following:

        [NUnit.Framework.TestAttribute()]
        [NUnit.Framework.DescriptionAttribute("Desc")]
        [NUnit.Framework.CategoryAttribute("SmokeTest")]
        [NUnit.Framework.CategoryAttribute("Search")]
        [NUnit.Framework.CategoryAttribute("batch1")]
        [NUnit.Framework.CategoryAttribute("batch2")]
        public virtual void Test()
        {
            this.Test();
        }

SmokeTest and Search are my @Tags in features
batch1 and batch2 from my app.config

Could anyone help me make this work?

Triet Nguyen

unread,
Jul 27, 2016, 8:40:28 AM7/27/16
to SpecFlow

I created a custom Generator Plugin for NUnit automatically insert CategoryAttribute into the test methods. 

However the NUnit framework does not support reads current executing category (the selected category from GUI nor Console) and automatically explicit other cats, so I could add custom tags but they cannot work as my expectation. 

This is a no go.

Gáspár Nagy

unread,
Jul 27, 2016, 8:59:08 AM7/27/16
to SpecFlow
I don't understand the problem. Could you describe it a bit more in detail please?

Triet Nguyen

unread,
Jul 27, 2016, 10:31:26 AM7/27/16
to SpecFlow
Hi Gáspár,

My customer wanted an ability to run tests on NUnit GUI with multiple Test Environment setups.

We created a configuration class and store all the setup configurations in the app.config as following example:

  <batches>
    <batch1>
      ConnectionString = "Con1", baseAddress="add1"
    </batch1>
    <batch2>
      ConnectionString = "Con2", baseAddress="add2"
    </batch2>
  </batches>

We're expecting when the BA/tester selected a category to run (i.e batch1) it will loads the configuration in the app.config under batch1 node, parses it to the BeforeTestRun hook to initialize environment and then starts all the tests.

But the problem is NUnit GUI only regconize the Specflow @Tags in scenarios and translate it to NUnitCategoryAttribute. That's why we have to use a custom generator to insert batch1 and batch2 as NUnitCategoryAttribute into the feature.cs file whenever custom tool runs and regenerate feature.cs files.

Now I successfully created the plugin and insert the categories, I found out NUnit framework does not support me query the current running category in TestContext, if I don't know which category is running, I can't load correct configurations for my test setup class. 
Further more, NUnit categories does not work as parameter targets for the test method.

That's why I said this is a no go and I have to looking for better solutions.

Gáspár Nagy

unread,
Jul 28, 2016, 2:51:04 AM7/28/16
to SpecFlow
Hi,

I see. I don't know if you are aware, but SpecFlow+ Runner has a built-in functionality that targets this scenario.

Generally, I would use app config settings to specify the target batch and not categories. You can also have a look at this post: https://johanleino.wordpress.com/2013/05/13/specflow-using-environment-specific-settings/

Br,
Gaspar

Triet Nguyen

unread,
Jul 28, 2016, 5:57:13 AM7/28/16
to SpecFlow
Hi,

Thank you for the suggestion, that's exactly what I looking for: using targets and isolate test threads when running. I did it successfully with MSTest.
But I think there is a problem with NUnit recognizing the SpecRunProfile. It can't show the tests on GUI with different targets.

I added a question on SpecRun but sadly there's no answer yet: https://groups.google.com/forum/#!topic/specrun/CrSssPnuQ44
Reply all
Reply to author
Forward
0 new messages