locked
Unit test vs NUnit code. RRS feed

  • Question

  • Hi Friends,

    I created few line of code which take screen shot of current webpage. I store the image at current directory of the solution. The code is developed in unit test project. And it goes to the directory which I want. Below is the code:

    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Support.UI;
    using System.Threading;
    using System.Drawing.Imaging;
    
    namespace WebDriverTest
    {
        [TestClass]
        public class IBLogin
        {
            public static IWebDriver objDriver;
    
            private TestContext testContextInstance;
            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }
    
            [TestInitialize]
            public void SetUp()
            {
                SelectBrowser.Initialize();
            }
    
            [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Credentials.csv", "Credentials#csv", DataAccessMethod.Sequential), DeploymentItem("Credentials.csv")]
            [TestMethod]
            public void Login()
            {
                try
                {
                    objDriver = SelectBrowser.webdriver;  
                    objDriver.FindElement(By.Id("overridelink")).Click();                
                    System.Threading.Thread.Sleep(900);
                }
                catch(Exception e)
                {
                    SaveScreenShot(objDriver.Title);
                    Assert.Fail(e.Message);
                }
                
            }
    
            [TestCleanup]
            public void TestCleanUp()
            {            
                objDriver.Quit();
            }
    
            public static void SaveScreenShot(string screenshotFirstName)
            {
                var folderLocation = Environment.CurrentDirectory;
                 folderLocation = Path.GetFullPath(Path.Combine(folderLocation, @"..\..\"));
                 folderLocation = folderLocation + "ScreenShot\\";
    
                if (!Directory.Exists(folderLocation))
                {
                    Directory.CreateDirectory(folderLocation);
                }
               // Few more line of code....
            }
    
        }
    }


    First line of function SaveScreenShot return this:

    Folder location  has my directory path.

    I was trying to run  the same code using NUnit framework. I am new to Nunit. I made the below changes:

    using System;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Support;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Interactions;
    using NUnit.Framework;
    using System.IO;
    using System.Text;
    
    namespace WebDriverTest
    {        
        public class NUnitExample
        {
            public static IWebDriver objDriver;
    
            private TestContext testContextInstance;
    
            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }
    
            [SetUp]
            public void SetUp()
            {
                SelectBrowser.Initialize();
            }
    
            [Test]
            public void TC_NUnitExample()
            {
                try
                {
                    objDriver = SelectBrowser.webdriver;
                    objDriver.FindElement(By.Id("overridelink")).Click();                                System.Threading.Thread.Sleep(900);
                }
                catch(Exception e)
                {
                    SaveScreenShot(objDriver.Title);
                    Assert.Fail(e.Message);
                }           
            }
    
            [TearDown]
            public void CleanUp()
            {
                objDriver.Quit();
            }
    
            public static void SaveScreenShot(string screenshotFirstName)
            {
                var folderLocation = Environment.CurrentDirectory;
                folderLocation = Path.GetFullPath(Path.Combine(folderLocation, @"..\..\"));
                folderLocation = folderLocation + "ScreenShot\\";
    
                if (!Directory.Exists(folderLocation))
                {
                    Directory.CreateDirectory(folderLocation);
                }
                // Few more lines of code
            }
        }
    }

    First line of function SaveScreenShot returns this:

    I am not sure why it is pointing to a different location. Any idea why it is happening when the function is copied from unit test cs file.

    Additionally I would like to know why I don't see any entries in test result folder of the solution when Nunit file [Test] are run. I do when [Test Method] of unit test file is run.

    Thanks for looking.


    Ahetejazahmad Khan.


    • Edited by Ahetejaz Monday, September 18, 2017 12:07 PM
    • Moved by KareninstructorMVP Monday, September 18, 2017 12:54 PM Moved from C# forum
    Monday, September 18, 2017 12:02 PM

Answers

  • Hi Ahetejaz,

    Thanks for your reply.

     

    Based on my knowledge, Environment.CurrentDirectory gets or sets the fully qualified path of the current working directory.

    Please reference: https://msdn.microsoft.com/en-us/library/system.environment.currentdirectory(v=vs.110).aspx

     

    In fact, Unit Test and NUnit has his own working directory. So, when you use Environment.CurrentDirectory, it’s pointing to different location.

     

    In addition, if you want to point to project location, I suggest you use “AppDomain.CurrentDomain.BaseDirectory” and give it a try.

     

     
    Regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Wednesday, September 20, 2017 1:48 AM
    • Proposed as answer by Fletch Zhou Wednesday, September 20, 2017 10:19 AM
    • Marked as answer by Ahetejaz Wednesday, September 20, 2017 12:06 PM
    Wednesday, September 20, 2017 1:48 AM

All replies

  • You are not doing unit testing.

    http://www.artima.com/weblogs/viewpost.jsp?thread=126923

    You are not posting about C# issues to the C# forum

    You should post to the appropriate forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsunittest

    Monday, September 18, 2017 12:19 PM
  • Hi Ahetejaz,

    Thanks for your posting.

    >> I am not sure why it is pointing to a different location. Any idea why it is happening when the function is copied from unit test cs file.

    >>Additionally I would like to know why I don't see any entries in test result folder of the solution when Nunit file [Test] are run. I do when [Test Method] of unit test file is run.

    To be honest, there is some overlap between NUnit and Visual Studio Unit Test. NUnit is the predecessor, and as a result more mature. But Unit Test is younger, it integrated with Visual Studio, and calls MStest framework. The bigger differences between them are:

    Unit Test requires a Visual Studio instance on the build server in order to run. It does not contain a standalone test runner like NUnit.

    Unit Test provides test results and code coverage results directly within Visual Studio. NUnit provides test results in its own UI and requires additional tools to perform code coverage analysis.

    And for the two tools, when you specified the Environment.CurrentDirectories, they pointed to two different ones, as they installed or belong to two different paths.

    Thanks for your understandings.  

     

    Regards,

    Fletcher



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Tuesday, September 19, 2017 5:29 AM
    Tuesday, September 19, 2017 5:29 AM
  • Hi Fletch,

    Thanks for the response. I have installed NUnit is the same project location. Package folder contains the Nunit folders.

    Please correct me if I am mistake to assume the solution location will be directory as Nunit folders are here.

    Thank you.


    Ahetejazahmad Khan.

    Tuesday, September 19, 2017 8:22 AM
  • Hi Ahetejaz,

    Thanks for your reply.

     

    Based on my knowledge, Environment.CurrentDirectory gets or sets the fully qualified path of the current working directory.

    Please reference: https://msdn.microsoft.com/en-us/library/system.environment.currentdirectory(v=vs.110).aspx

     

    In fact, Unit Test and NUnit has his own working directory. So, when you use Environment.CurrentDirectory, it’s pointing to different location.

     

    In addition, if you want to point to project location, I suggest you use “AppDomain.CurrentDomain.BaseDirectory” and give it a try.

     

     
    Regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Wednesday, September 20, 2017 1:48 AM
    • Proposed as answer by Fletch Zhou Wednesday, September 20, 2017 10:19 AM
    • Marked as answer by Ahetejaz Wednesday, September 20, 2017 12:06 PM
    Wednesday, September 20, 2017 1:48 AM
  • Thanks for the response Fletch. I tried it and it works fine for both type of tests.

    Thank you,


    Ahetejazahmad Khan.


    • Edited by Ahetejaz Wednesday, September 20, 2017 12:42 PM
    Wednesday, September 20, 2017 12:06 PM