NUnit: How can I set name of test depending on parametr in [TestFixture(typeof(param))]

543 Views Asked by At

I'm using NUnit + Webdriver + C#. Setup class has next stucture:

[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(ChromeDriver))]
public partial class SetupBase<TWebDriver> where TWebDriver : IWebDriver, new()
{       
    public IWebDriver _driver;
    [OneTimeSetUp]
    public void OneTimeSetUp()
    {  
        Init();
    }
 }

How can I set name of tests to include methode name, arguments and name of browser?

I tried with capabilities but it didn't help

ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("Name", String.Format ("{0}_Chrome", TestContext.CurrentContext.Test.Name), true);

Also tried to use next code but was not able to find way how to pass driver type to NameAttribute

public class NameAttribute : NUnitAttribute, IApplyToTest
    {
        public NameAttribute(string name)
        {
            Name = String.Format("{0} {1}", name);
        }

        public string Name { get; set; }

        public void ApplyToTest(Test test)
        {
            test.Properties.Add("Name", Name);
        }
    }

Can you help me please. Maybe need to update base class structure somehow?

This is how I use in tests

public class _001_General<TWebDriver> : SetupBase<TWebDriver> where TWebDriver : IWebDriver, new()
    {     
        [OneTimeSetUp]
        public void OneTimeSetupTest ()
        {
            //somework
        }

        [Test]
        public void Test ()
        {
            //somework
        }
     }

Also SetupBase class contains functions that are used in tests

1

There are 1 best solutions below

2
On

In NUnit, test cases, test methods, test fixtures and generic test fixture classes are all "tests" even though we sometimes talk loosely about "tests" as meaning test cases.

In your example, the following names are created:

_001_General<TWebDriver> (or maybe _001_General<>)
    _001_General<InternetExplorerDriver>
        Test
    _001_General<ChromeDriver>
        Test

Tests also have "fullnames", similar to that for a type or method. For example

_001_General<ChromeDriver>.Test

(Note: the fullname would also include a namespace, which I haven't shown.)

If you are using a runner that displays fullnames, like the NUnit 3 Console Runner, then there is no problem. So, I assume you are not. :-)

Some runners, like the NUnit 3 Visual Studio Test Adapter use simple test case names. So you would end up with a bunch of tests displayed with the name "Test."

Is that your problem? If so, this is not much of an answer. :-) However, I'll leave it as partial and add to it after hearing what runner you want to use and what you would like to see displayed in it.

UPDATE:

Based on your comment, what you really want to see is the test FullName - just as it is displayed by the NUnit 3 Console runner that TC runs for you. You'd like to see them in the VS IDE using the NUnit 3 VS Adapter.

Unfortunately, you can't right now. :-) More on that below. Meanwhile, here are some workarounds:

  1. Use the console runner on your desktop. It's not as visual but works quite well. It's how I frequently work. Steps:

    • Install the console runner. I recommend using Chocolatey to install it globally, allowign you to use it with all your projects.
    • Set up your project to run the console runner with any options you like.
    • Make sure you use an external console window so you get the color display options that make the console runner easier to use.
    • Size your windows so you can see everything (if you have enough screen space) or just let the console run pop up on top of VS.
  2. Try to trick VS by setting the test name in a way that includes the driver parameters. That's what you are already doing and it sounds as if you have already gotten almost all you can out of this option, i.e. class name without class parameters. You could try to take it a step further by creating separate classes for each driver. This would multiply the number of classes you have, obviously, but doesn't have to duplicate the code. You could use inheritance from the generic classes to create a new class with only a header in each place where it's needed. Like...

    public class TestXYZDriver : TestDriver ...

This might be a lot of work, so it really depends on how important it is to you to get visual results that include fixture parameters right now.


For the future, you could request that the NUnit 3 Adapter project give an option of listing tests by their full names. I haven't worked on that project for a few years, so I'm not sure if it's actually possible. It may not be entirely in the control of the adapter, since VS controls what is displayed.