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).
Related
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.
In Visual Studio 2015 V14 Update3 with NUnit Adapter 3.10.0.21 and NUnit Framework 3.10.1, the Visual Studio Test Explorer shows tests with sources, but some tests cannot be run via T.Explorer.
Visual Studio - Test Explorer
After running all tests, not all tests were run:
To select one of the last two tests and running it just yields no result, and a fairly useless messages in the Tests Output window:
------ Run test started ------
NUnit Adapter 3.10.0.21: Test execution started
Running selected tests in C:\TFS\TestFactory\TA\DA\DAGICom\bin\Debug\DAGICom.exe
NUnit3TestExecutor converted 5 of 5 NUnit test cases
NUnit Adapter 3.10.0.21: Test execution complete
========== Run test finished: 0 run (0:00:02,49) ==========
I solved, the problem depends on the length of a string passed to the test method.
With the previous combination Nunit.Framework ("3.2.0") and NUnit3TestAdapter (3.0.10) there was not this problem.
Currently,It seems as if the maximum string fixed-length is 850 characters.
max fixed-length(result) = 850 characters.
[Test(Author = "Michele Delle Donne"), Description("")]
[TestCaseSource("TC_XXXX_XXXXXXXXXX"), Category("XXXXX")]
public void DA_ACOM(Type testClass, string environment, string user, string pwd, string result)
{
Services.ObjBase automationTest = null;
object[] args = new object[] { Settings_Default.browser, environment, testClass.ToString(), testClass.ToString(), result };
automationTest = (Services.ObjBase)Activator.CreateInstance(testClass, args);
if (automationTest != null)
{
automationTest.ExecuteAutomation(environment, user, pwd);
}
Thread.Sleep(TimeSpan.FromSeconds(1));
automationTest.End();
}
I am checking TestContext.CurrentTestOutcome in my TestCleanup method in order to perform an action if the test did not pass (in this case, the tests are using Selenium to exercise a website and I am saving a screenshot if the test does not pass).
private static TestContext _testContext;
private static IWebDriver _driver;
[ClassInitialize]
public static void SetupTests(TestContext testContext)
{
_testContext = testContext;
_driver = new FirefoxDriver();
}
[TestCleanup]
public void TeardownTest()
{
if (_testContext.CurrentTestOutcome != UnitTestOutcome.Passed)
{
var fileName = Path.Combine(
Environment.CurrentDirectory,
string.Format("{0}.{1}.gif", _testContext.FullyQualifiedTestClassName, _testContext.TestName));
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(fileName, ImageFormat.Gif);
Console.WriteLine("Test outcome was {0}, saved image of page to '{1}'", _testContext.CurrentTestOutcome, fileName);
}
}
This works well when run on a local development PC using ReSharper, but on our build server (which uses TeamCity) the UnitTestOutcome is always Unknown, although TeamCity reports them as passed.
The documentation on MSDN is not very helpful. What can cause this value to be set to Unknown?
According to http://confluence.jetbrains.com/display/TCD8/MSTest+Support TeamCity does not support on-the-fly reporting of individual test results, it parses the tests results file to provide the results to the build step.
That would explain how TeamCity is able to report the tests as passed even though UnitTestOutcome may be unknown at the time an individual test has completed.
The link above mentions "specifics of MSTest tool" as the reason for non-on-the-fly test result reporting so I can only theorize that the same specifics may mean that TestContext is unavailable when running from your build server.
Also, the MSDN documentation for TestContext.CurrentTestOutcome does mention that Full Trust for the immediate caller is required. TeamCity could be executing the tests in a manner that is only partially trusted and therefore causing the test outcome to be Unknown.
A quick way to check if MSTest is your problem would be to switch to NUnit using:
#if NUNIT
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using IgnoreAttribute = NUnit.Framework.IgnoreAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IgnoreAttribute = Microsoft.VisualStudio.TestTools.UnitTesting.IgnoreAttribute;
#endif
source http://www.anotherchris.net/tools/using-team-city-for-staging-and-test-builds-with-asp-net-and-selenium/
You would have to do something similar in your TeardownTest method to use the NUnit TestContext.CurrentContext.Result.Status though.
The fix for this issue is to use a public property for TestContext, rather than using the parameter passed to the [ClassInitialize] method.
i.e.
public TestContext TestContext { get; set; }
The test runner will automatically set the property.
(This is related to another question I posted on SO)
When i try to run my tests from Visual Studio 2012 Ultimate i get this output
------ Discover test started ------
========== Discover test finished: 0 found (0:00:05.8242806) ==========
Here is the code:
[Then(#"the submitter company list is in alphabetical order")]
public void ThenTheSubmitterCompanyListIsInAlphabeticalOrder()
{
List<string> submitterCompanyList = _currentFilingPage.SubmitterCompanyList;
submitterCompanyList.Should().BeInAscendingOrder();
}
I have created a .bat file and from there i can run my tests. Please assits me with a way to run my tests from visual studio. (Extra Information: I can't see my tests on Test Explorer)
All the Tests must have the TestMethodAttribute so MSTest/Visual Studio can find them.
see: MSDN Anatomy of a Unit Test
So edit your code and add the TestMethod to your method so it can be found as a Test. Also your method must be added inside a TestClass:
[TestClass]
public class TestClass
{
[TestMethod]
public void ThenTheSubmitterCompanyListIsInAlphabeticalOrder()
{
}
}
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.