Can't run a simple Unit Test #Cs #VS19 #MSTest - c#

I'm new in unit test and I'm trying to run a simple test but after push the "run test" button it's load then nothing
What's wrong ????!!
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestNinja.Fundamentals;
namespace TestNinga.UnitTests
{
[TestClass]
public class ReservationTests
{
[TestMethod]
public void CanBeCancelledBy_UserIsAdmin_ReturnTrue()
{
var reservation = new Reservation();
var result = reservation.CanBeCancelledBy(new User { IsAdmin = true });
Assert.IsTrue(result);
}
}
}

It looks like nothing is wrong. Sometimes this happens also to me and sometimes I fix it by closing the VS, deleting the bin & obj folders, opening the VS, and cleaning the solution. One time I also noted out that the process runs in the background and stuck and as a result, the tests didn't run, so I killed the process, and then it started working.
If it won't help you always can see what is the error you get and share more information.
To see the error you should open the Output pane and in the Show output from: box, you should choose the tests option.

I find the isssue , I just update/add this package in my test solution :
Microsoft.NET.Test.Sdk
MSTest.TestAdapter
MSTest.TestFramework

Related

How to assert in selenium webdriver in C#?

I am working with Selenium WebDriver in C# and I have to create a service for an applicant. I have done this already but after I confirm the service goes to a List ( Services that need to be confirmed from another user ) which is increased by 1 in read mode. Is there any way how to assert these values that are increased by 1 every time a new service is added?
You need to use a test framework to do this -- selenium itself cannot assert for you.
If you are using C#, I recommend installing NUnit. You can find this under NuGet Package manager, and you'll also want to install NUnitTestAdapter if you are using Visual Studio.
Once you have installed a test framework on your project, you can use [Test] flags to designate entry point methods for test cases, and use Assert statements which are part of the NUnit namespace.
You can find documentation here: https://github.com/nunit/docs/wiki/NUnit-Documentation
Selenium's built-in assert functionality only exists in SeleniumIDE, the point-and-click browser add-on available for Chrome and Firefox.
If you are going to write your tests in C#, as Christine said, you need to use a unit testing framework. For example, I'm using Xunit, and a simple test looks like this:
using Xunit; // Testing framework. NuGet package
using OpenQA.Selenium.Firefox; // Driver for Firefox
using Xunit.Priority; // NuGet add-on to Xunit that allows you to order the tests
using OpenQA.Selenium; // NuGet package
using System.Diagnostics; // Can Debug.Print when running tests in debug mode
namespace Test_MyWebPage
{
[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)] // Set up ordering
public class Test_BasicLogin : IDisposable
{
public static IWebDriver driver = new FirefoxDriver(#"path\to\geckodriver");
// Here be the tests...
[Fact, Priority(0)]
public void Test_LaunchWebsite()
{
// Arrange
var url = "https://yourserver.yourdomain/yourvirtualdir";
// Act
// Sets browser to maximized, allows 1 minute for the page to
// intially load, and an implicit time out of 1 minute for elements
// on the page to render.
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().PageLoad = new TimeSpan(0, 1, 0);
driver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 1, 0);
driver.url = url; // Launches the browser and opens the page
/* Assuming your page has a login prompt
/* we'll try to locate this element
/* and perform an assertion to test that the page comes up
/* and displays a login prompt */
var UserNamePrompt = driver.FindElement(By.Id("userLogin_txtUserName"));
// Assert
Assert.NotNull(UserNamePrompt); // Bombs if the prompt wasn't found.
Debug.Print("Found User Name Prompt successfully.");
}
public void Dispose()
{
// Properly close the browser when the tests are done
try
{
driver.Quit();
}
catch (Exception ex)
{
Debug.WriteLine($"Error disposing driver: {ex.Message}");
}
}
}
}
As you can see, there is considerably more work in setting up tests for the Selenium WebDriver than in setting up simple smoke tests with the SeleniumIDE. I haven't addressed how you should store your configs properly (hardcoding as in the example is bad) and you will have to tailor your driver.find() statements to fit your precise situation. I am using the Xunit.Priority package so I can make sure the tests don't all run in parallel; I need to test one thing at a time in a progression. Your needs might be met by putting all of the steps into a single Test_* method. Each method appears as a separate test in the Visual Studio Test Explorer window. Right-clicking a test in the Test Explorer and selecting 'Debug selected tests' will allow you to set breakpoints and also enable your Debug.Print (or Debug.Write/Writeline) methods to display in the Tests section of the VS output window.
Another gotcha is in setting up the IWebDriver: don't put the complete path including the executable in the path, just the path to the folder that contains it.
Good luck and happy testing!

OneTimeTearDown is not working using selenium WebDriver

This code should takes a screenshot when test fail:
[TestClass]
public class UnitTest1
{
[OneTimeTearDown]
public void TestFail()
{
IWebDriver driver = new ChromeDriver();
if (NUnit.Framework.TestContext.CurrentContext.Result.Outcome != ResultState.Success)
{
string screensLocation = #"D:\";
string testName = NUnit.Framework.TestContext.CurrentContext.Test.Name;
var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile(screensLocation + testName + ".png");
}
}
[TestMethod]
public void TestMethod1()
{
// my code, here test is failed
}
}
But it is not working. I don't have any screen in location D:\
Otherwise is there a way to debug code under OneTimeTearDown Attribute? Because when the test is fail, debugging ends and I don't know what's going on in the method TestFail().
Thanks for your help.
OneTimeTearDownAttribute is a feature of NUnit.
Although your tag says "nunit", your code is not actually using it. TestClassAttribute and TestMethodAttribute are features of MS Test. If you tried to run this test with NUnit, it would not recognize the tests at all.
Obviously, your test assembly does reference the NUnit framework, since it would not otherwise compile.
So... bottom line, your test code references two different frameworks in such a way that it cannot be run successfully by either runner!!! You have to choose which of the two you want to use, remove the other reference and use a runner for the framework you choose to keep.

How to open a browser and keep it open between coded UI tests in Visual Studio 2017 using C#

I am trying to make some coded UI tests to help automate some of the manual testing at the company I work at. I am pretty new to the CUIT part of visual studio, but I feel like I am figuring it out. However I am having an issue with the testing thread being closed before the other tests run.
So I want to make this testing fully automated, as in, all the developer will need to do is to click "Run all" and they will all run automatically. The problem that I am having is that the very first test needs to launch Internet Explorer, go to a website, and log into the website. The rest of the tests are based off of being logged into the system. However, after the first test, the browser closes and gets killed along with the first test method.
Any advice on this would be great, I have searched online for some answers but a lot are for very old versions of visual studio, and the ones I have tried don't work.
Thank you.
Edit: So inside each class lets say "CodedUITest1.cs", I can use the same browser in each of the [Test Method]s that I have in that class (as someone suggested below). The issue I have is that if I want a different test class to test different functionality, "CodedUITest2.cs", the browser will close when the first class finishes its tests.
If I'm understanding your question correctly, then This code segment should work for you:
BrowserWindow window;
[TestMethod]
public void Method1()
{
window = BrowserWindow.Launch(new Uri("http://www.bing.com"));
window.CloseOnPlaybackCleanup = false;
}
[TestMethod]
public void Method2()
{
window = BrowserWindow.Locate("Bing");
window.CloseOnPlaybackCleanup = false;
}
[TestMethod]
public void Method3()
{
window = BrowserWindow.Locate("Bing");
}
After reading the new info of this question, I have tested the code a bit. If you want to keep the browser open between CodeUITes1.cs and CodedUITest2.cs, then the following code segment may help you. It is adopted from the following link: https://blogs.msdn.microsoft.com/devops/2012/11/08/coded-ui-test-why-does-application-close-after-each-test-in-visual-studio-2012/
File: CodedUITest1.cs
public class CodedUITest1
{
static BrowserWindow browserWindowInstance = null;
public void LoadLocalHost()
{
if (browserWindowInstance == null)
{
browserWindowInstance = BrowserWindow.Launch(new System.Uri("YourWebSiteAddress"));
browserWindowInstance.CloseOnPlaybackCleanup = false;
browserWindowInstance.Maximized = !browserWindowInstance.Maximized;
}
else
{
browserWindowInstance.Maximized = !browserWindowInstance.Maximized;
}
}
[TestMethod]
public void CodedUITestMethod1()
{
LoadLocalHost();
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
this.UIMap.ClickNewsAndEvents();
}
CodedUITest2.cs file:
[TestMethod]
public void CodedUITestMethod2()
{
CodedUITest1 obj1 = new CodedUITest1();
obj1.LoadLocalHost();
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
this.UIMap.ClickNewsPage();
}
You can add more CodedUITest classes. Just create a new object like obj1 in the code sample of CodedUITest2 class, and use LoadLocalHost() method that resides in CodedUITest1.class from any subsequent classes. Hoping this will resolve your problem.

Slow execution of tests using Teststack.White + Nunit3-console

Hello i have small problem with nunit and teststack white.
Here i have small test (Window is localized with option WithCache):
[Test] public void ShouldLogIn()
{
var loginPanel = Window.Get<Panel>("txbLogin");
var loginTextBox = loginPanel.Get<TextBox>("mobTextBox1");
loginTextBox.BulkText = "user1";
}
Now i run this test from VS, and debugging each step. I can notice, that Get takes around 50ms, Get 30ms. Next i run the same test using nunit3-console.exe and now, after attaching to process i can notice around 3 time longer execution of each step. Its not problem with attaching, becouse i checked it in longer test and always steps takes longer.
Another long process is getting new window. My application show new windows, which covers actual one, so i need to get new window when new one appears. Command Application.GetWindows().Last() takes around 200-300ms from VS, but from nunit console in worst case can take about 3 seconds.
Maybe someone of you have similar problem and have any solution for me.
HERE I HAVE EXAMPLE:
You need to download app from here:
https://www.codeproject.com/Articles/607868/Social-Club-Sample-application-using-WinForms-Csha
Build it, run application, login into it with credentials:
login: demo
password: demo123
Then create new unit test prkject, add nuget pacakges for nunit and teststack.white. Here is code for this test:
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Castle.Core.Logging;
using NUnit.Framework;
using TestStack.White;
using TestStack.White.Configuration;
using TestStack.White.Factory;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Finders;
using TestStack.White.UIItems.WindowItems;
namespace StackTestProject
{
[TestFixture]
public class Notepadtests
{
private Application Application;
private Window Window;
[OneTimeSetUp]
public void OneTimeInitialize()
{
CoreAppXmlConfiguration.Instance.FindWindowTimeout = 30000;
CoreAppXmlConfiguration.Instance.LoggerFactory = new WhiteDefaultLoggerFactory(LoggerLevel.Debug);
}
[SetUp]
public void TestInitialize()
{
Thread.Sleep(5000);
ProcessStartInfo psi = new ProcessStartInfo("John.SocialClub.Desktop.exe");
psi.WorkingDirectory = #"C:\Users\[USER NAME]\Downloads\John.SocialClub\John.SocialClub\John.SocialClub.Desktop\bin\Debug";
Application = TestStack.White.Application.AttachOrLaunch(psi);
//before this u need to have opened and logged in aplication with login:demo and password: demo123
Window = Application.GetWindow("Social Club - Membership Manager", InitializeOption.WithCache); //250ms
}
[Test]
public void NotepadTest()
{
var toolbar = Window.Get<Panel>(SearchCriteria.ByClassName("tabRegistration")); //33ms
}
[TearDown]
public void TestCleanup()
{
Application.Kill();
}
}
}
I hit breakpoint on line where i assign value to Window, and now i check time of getting window with name "Social Club - Membership Manager":
a) when i debug test from visual studio - its around 450ms for first assign, when i move cursor back to this line and assign again its faster and takes about 250ms
b) when i run from nunit3-console with command :
nunit3-console.exe C:\Users\kamil.chodola\source\repos\StackTestProject\StackTestProject\bin\Debug\StacktestProject.dll
and attach to proces nunit-agent.exe program stops on breakpoint and now assign of Window take around 1second, second assign takes around 700ms which is still longer than running same test from visual studio adapter.
I must notice that its not problem with attaching to proces, becouse without attaching i can see as well, that this test takes longer than from visual studio.
Really interesting thing is that i reproduce this issue only on winforms applications. When i automatize app like notepad which is base on Win32 framework times are normal or even faster. Same thing on web application, and automatizing with selenium webdriver.

Outputting text in unit tests

When I run my unit tests, I would like to print out and read how long it takes to run a function. I tried using Console.WriteLine() and Trace.WriteLine(), but that didn't work. What is the proper method I should be using?
I have the following unit test
[TestMethod()]
public void ProductSerializationTest()
{
Stopwatch swSerialization = new Stopwatch();
swSerialization.Start();
SerializeProductsToXML(dummyProductList, XMLFolderPath);
swSerialization.Stop();
// Print out swSerialization.Elapsed value
}
If you're using Visual Studio with its built-in testing support, the output of System.Diagnostics.Trace.WriteLine will go to the test results report. That is, after your test has run, double-click its entry in the Test Results list, and there will be a section called "Debug Trace" in the test run report.
I had the same problem. I eventually found the solution.
Notice the green check or red x when you run the test in Visual Studio? Now click that and then click the link that says output.
That shows all the Trace.WriteLine()s for me, even when not in debug mode.
Since you're using Microsoft.VisualStudio.TestTools.UnitTesting, it would be most reasonable to call TestContext.WriteLine(). Here's how to use a TestContext.
Since I use TestDriven and NUnit, I just use Console.WriteLine() and the messages show when I run tests with the debugger.

Categories