Outputting text in unit tests - c#

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.

Related

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

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

Visual Studio Code unit test marked with question mark, no matter what the result is

In Visual Studio Code, I have a specific unit test that no matter what the result is - is marked with a question mark.
I added other tests and they behave as expected (marked with either a green tick or red x).
What does the question mark mean ?
[Fact]
public async Task LinuxHelper_ExecuteInvalidCommandBash_ShouldFail()
{
//arrange
//act
//var bashExecutionResult = LinuxHelper.RunAsBash(#"dasda");
//assert
10.Should().Be(11);
//bashExecutionResult.Succeeded.Should().BeFalse();
//bashExecutionResult.Error.Should().NotBeNullOrEmpty();
//bashExecutionResult.ExitCode.Should().NotBe(0);
}
Using C#, .NET Core Test Explorer VSC extension, with xUnit and FluentAssertions.
For me this occurred when I hadn't set a glob pattern for the Dotnet-test-explorer.testProjectPath setting, and had multiple test projects in my solution. Once I set that option to **/*.Test.csproj it could manage the results from running all of the tests.

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!

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.

NUnit Adapter 3.9.0.0 input string truncating

I have next test method for NUnit:
[Test]
[TestCase("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")]
[TestCase("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaZZ")]
public void NUnitExploringTesting(string someString)
{
throw new System.NotImplementedException();
}
and I retrieve next messages in Output window:
------ Run test started ------
NUnit Adapter 3.9.0.0: Test execution started
Running all tests in ...
NUnit3TestExecutor converted 8 of 8 NUnit test cases
NUnit Adapter 3.9.0.0: Test execution complete
Contents of string 'NUnitExploringTesting("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaZZ")'
exceeds max of '449', string has been truncated.
Can anybody explain me what's wrong and how to increase max length of string parameters from 449 to 1024?
Unfortunately, as explained in the issues cited by two commenters, the 449 character limitation is built into the Test Explorer and is out of NUnit's control as well as yours. All we can do is try to shorten the name so that VS is not bothered by it.
In version 3.9.0 of NUnit, you can use the TestName property of TestCase to modify the display name of the test.
[TestCase("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
TestName="LotsOfAs")]
[TestCase("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaZZ",
TestName="LotsOfAsWithTwoZsAtEnd")]
public void NUnitExploringTesting(string someString)
{
throw new System.NotImplementedException();
}
The tests will show up with the names LotsOfAs and LotsOfAsWithTwoZsAtEnd.
As a further refinement, you can use a name like "%m(LotsOfAs)", which will display as NUnitExploringTesting(LotsOfAs).

Categories