How to set TestName in CodedUI? - visual-studio-2012

I have a data driven CodedUI test method named myTestMethod, which uses XML for supplying input data.
For each run on a data set, CodedUI reports something like this in the Test Explorer:
Test Passed - myTestMethod (Data Row 0)
Test Passed - myTestMethod (Data Row 1)
Test Failed - myTestMethod (Data Row 2) <error details>
Test Failed - myTestMethod (Data Row 3) <error details>
I was wondering is there is a way to set the test name to something more identifiable (probably from the input data set itself).
Seems like CodedUI uses TestContext.TestName for this reporting purpose, but, it is a readonly property. Is there a way to set it somewhere else, somehow?
Please help.
Thanks,
Harit

Ok i understand you now. I actually have a class where i write general functions. One of them is for save the test results as i want.
I use data driven based in XML. then my Employe1 and Employe2 are different runs of the same test_method.
Just locate [TestCleanup()] and call here a function to save a log has you need.
Log can be saved in csv format, plain text separated by ; for example with StreamWriter.
namespace NAME_SPACE
{
[CodedUITest]
public class Program_Something_BlaBla
{
Stopwatch stopWatch = new Stopwatch();
[TestMethod(), Timeout(999999999)]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", PATH_XML, "DATOS", DataAccessMethod.Sequential)]
public void Program_Something_BlaBla_Method()
{
string employe = TestContext.DataRow["EMPLOYE"].ToString();
try
{
//Test actions
...
{
catch (Exception g)
{
...
return;
}
}
#region Additional test attributes
// You can use the following additional attributes as you write your tests:
////Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize()
{
stopWatch.Start();
...
}
//Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup()
{
stopWatch.Stop();
...
Common.EndTest(employe);
}
#endregion
Hope it helps,

I don't want to say it's impossible, but the Test Explorer in populated at compile time and the data would be pulled in at run time.

I don't think this would be possible. The only way for you is to read the generated "TRX" file ( as it is xml) and replace the values by reading your csvs. Might need to do a tool but the overhead is huge.

Related

How do I get a parameter to not just display in a component, but also be recognized inside of OnInitializedAsync()?

I'm working on a blazor server-side project and I have a component that gets passed a model (pickedWeek) as a parameter. I can use the model fine in-line with the html, but OnInitializedAsync always thinks that the model is null.
I have passed native types in as parameters, from the Page into a component, this way without an issue. I use a NullWeek as a default parameter, so the number getting used in OnInitializedAsync only ever appears to be from the NullWeek. In case this is related, there is a sibling component that is returning the Week model to the Page through an .InvokeAsync call, where StateHasChanged() is being called after the update. It appears that the new Week is getting updated on the problem component, but that OnInitializeAsync() either doesn't see it, or just never fires again- which maybe is my problem, but I didn't think it worked that way.
For instance, the below code will always show "FAILURE" but it will show the correct Week.Number. Code below:
<div>#pickedWeek.Number</div>
#if(dataFromService != null)
{
<div>SUCCESS</div>
}
else
{
<div>FAILURE</div>
}
#code{
[Parameter]
public Week pickedWeek { get; set; }
protected IEnumerable<AnotherModel> dataFromService { get; set; }
protected override async Task OnInitializedAsync()
{
if (pickedWeek.Number > 0)
{
dataFromService = await _injectedService.MakeACall(pickedWeek.Id);
}
}
}
#robsta has this correct in the comments, you can use OnParametersSet for this. Then, you will run into another issue, in that each rerender will set your parameters again and generate another call to your service. I've gotten around this by using a flag field along with the the OnParametersSet method. Give this a shot and report back.
private bool firstRender = true;
protected override async Task OnParametersSetAsync()
{
if (pickedWeek.Number > 0 && firstRender)
{
dataFromService = await _injectedService.MakeACall(pickedWeek.Id);
firstRender = false;
// MAYBE call this if it doesn't work without
StateHasChanged();
}
}
Another alternative is to use the OnAfterRender override, which supplies a firstRender bool in the the method signature, and you can do similar logic. I tend to prefer the first way though, as this second way allows it to render, THEN sets the value of your list, THEN causes another rerender, which seems like more chatter than is needed to me. However if your task is long running, use this second version and build up a loading message to display while the list is null, and another to display if the service call fails. "FAILURE" is a bit misleading as you have it as it's being displayed before the call completes.
I've also found that a call to await Task.Delay(1); placed before your service call can be useful in that it breaks the UI thread loose from the service call awaiter and allows your app to render in a loading state until the data comes back.

Revit Api Load Command - Auto Reload

I'm working with the revit api, and one of its problems is that it locks the .dll once the command's run. You have to exit revit before the command can be rebuilt, very time consuming.
After some research, I came across this post on GitHub, that streams the command .dll into memory, thus hiding it from Revit. Letting you rebuild the VS project as much as you like.
The AutoReload Class impliments the revit IExteneralCommand Class which is the link into the Revit Program.
But the AutoReload class hides the actual source DLL from revit. So revit can't lock the DLL and lets one rebuilt the source file.
Only problem is I cant figure out how to implement it, and have revit execute the command. I guess my C# general knowledge is still too limited.
I created an entry in the RevitAddin.addin manifest that points to the AutoReload Method command, but nothing happens.
I've tried to follow all the comments in the posted code, but nothing seems to work; and no luck finding a contact for the developer.
Found at: https://gist.github.com/6084730.git
using System;
namespace Mine
{
// helper class
public class PluginData
{
public DateTime _creation_time;
public Autodesk.Revit.UI.IExternalCommand _instance;
public PluginData(Autodesk.Revit.UI.IExternalCommand instance)
{
_instance = instance;
}
}
//
// Base class for auto-reloading external commands that reside in other dll's
// (that Revit never knows about, and therefore cannot lock)
//
public class AutoReload : Autodesk.Revit.UI.IExternalCommand
{
// keep a static dictionary of loaded modules (so the data persists between calls to Execute)
static System.Collections.Generic.Dictionary<string, PluginData> _dictionary;
String _path; // to the dll
String _class_full_name;
public AutoReload(String path, String class_full_name)
{
if (_dictionary == null)
{
_dictionary = new System.Collections.Generic.Dictionary<string, PluginData>();
}
if (!_dictionary.ContainsKey(class_full_name))
{
PluginData data = new PluginData(null);
_dictionary.Add(class_full_name, data);
}
_path = path;
_class_full_name = class_full_name;
}
public Autodesk.Revit.UI.Result Execute(
Autodesk.Revit.UI.ExternalCommandData commandData,
ref string message,
Autodesk.Revit.DB.ElementSet elements)
{
PluginData data = _dictionary[_class_full_name];
DateTime creation_time = new System.IO.FileInfo(_path).LastWriteTime;
if (creation_time.CompareTo(data._creation_time) > 0)
{
// dll file has been modified, or this is the first time we execute this command.
data._creation_time = creation_time;
byte[] assembly_bytes = System.IO.File.ReadAllBytes(_path);
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assembly_bytes);
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass && type.FullName == _class_full_name)
{
data._instance = Activator.CreateInstance(type) as Autodesk.Revit.UI.IExternalCommand;
break;
}
}
}
// now actually call the command
return data._instance.Execute(commandData, ref message, elements);
}
}
//
// Derive a class from AutoReload for every auto-reloadable command. Hardcode the path
// to the dll and the full name of the IExternalCommand class in the constructor of the base class.
//
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class AutoReloadExample : AutoReload
{
public AutoReloadExample()
: base("C:\\revit2014plugins\\ExampleCommand.dll", "Mine.ExampleCommand")
{
}
}
}
There is an easier approach: Add-in Manager
Go to Revit Developer Center and download the Revit SDK, unzip/install it, the check at \Revit 2016 SDK\Add-In Manager folder. With this tool you can load/reload DLLs without having to modify your code.
There is also some additional information at this blog post.
this is how you can use the above code:
Create a new VS class project; name it anything (eg. AutoLoad)
Copy&Paste the above code in-between the namespace region
reference revitapi.dll & revitapiui.dll
Scroll down to AutoReloadExample class and replace the path to point
your dll
Replace "Mine.ExampleCommand" with your plugins namespace.mainclass
Build the solution
Create an .addin manifest to point this new loader (eg.
AutoLoad.dll)
your .addin should include "FullClassName" AutoLoad.AutoReloadExample
This method uses reflection to create an instance of your plugin and prevent Revit to lock your dll file! You can add more of your commands just by adding new classes like AutoReloadExample and point them with seperate .addin files.
Cheers

Create shared NUnit tests for plugins

I have plugins and i need to test, that any plugin fits to some specification. One of these cases is to check whether some interface exists in assembly (need to reflect from assembly).
I'd like to create some console application which will take plugin as an argument and check it.
This application will contain a set of tests, that will be configured by a passed argument. And the test runner which will produce xml report to output.
Is there better solution?
Update.
In my console application i call:
static int Main(string[] args)
{
CoreExtensions.Host.InitializeService();
var runner = new SimpleTestRunner();
var testPackage = new TestPackage(Assembly.GetExecutingAssembly().FullName);
string loc = Assembly.GetExecutingAssembly().Location;
testPackage.Assemblies.Add(loc);
if (runner.Load(testPackage))
{
var result = runner.Run(new NullListener(), new AllTestsFilter(), false, LoggingThreshold.Off);
var buffer = new StringBuilder();
new XmlResultWriter(new StringWriter(buffer)).SaveTestResult(result);
Console.Write(buffer.ToString());
return result.IsSuccess
? 0
: -1;
}
return -1;
}
In this soultion i have tests, but i need to pass arguments from command line to this tests through runner..
Probably you can use the TestCaseSource attribute: http://nunit.org/index.php?p=testCaseSource&r=2.6.3
Inside the test case source property you can enumerate the assemblies to test: NUnit will take care to generate a parametric test for each value.
Regarding the command line execution, you can use nunit-console.exe. You can get it here: http://www.nuget.org/packages/NUnit.Runners/
Hope it helps.
solved this problem by creation simple console application without NUnit.. Just return code -1/0

NUnit TestCaseSource modification

I have some code that looks like this
[Test, TestCaseSource("NspecRunner")]
public void TextContext (example thing)
{
//does testing like things using thing
while( othread.threadState == running && currentTestCount == StaticList.count)
Thread.Sleep(1000)
}
public List<example> NspecRunner
{
ClassName x = new ClassName
Thread othread = new Thread(new ThreadStart(() => x.function_that_adds_to_StaticList()));
return ClassName.StaticList;
}
However Nunit doesn't ever look at the StaticList that is being returned after the initial look. Do you know of any way to make the TestCaseScenario reevaluate so that it will create a new test as the other thread adds new stuff to that static class. I'm totally open to other frameworks. Or if you know where in the Nunit project I can find a way to modify the way that TestCaseSource works, that would be lovely.
edit:
I'm trying to create a test for each item inside of ClassName.StaticList. I don't know how many are going to be in the list, but I would like each test to start immediately after the item is added to the StaticList by the other thread. Is that possible?

AssertWasCalled on method in SystemUnderTest

I'm getting into TDD; using nUnit and RhinoMocks 3.5.
I'm trying to figure out how to AssertWasCalled on a method in the SystemUnderTest (SUT). My understanding is that you can't mock the system under test. In fact, my current test results in an exception because the I'm using the AssertWasCalled on the SUT.
OrdersPresenter:
public void OnViewLoad_GetOrders()
{
var orders = GetOrders();
View.Model.Orders = orders;
}
public List<Orders> GetOrders()
{
return _ordersRepository.GetAll();
}
OrdersPresenterTest:
_ordersPresenter = new OrdersPresenter(_view, _ordersRepository);
[Test]
public void OnViewLoad_GetOrders_Should_Call_GetOrders()
{
_view.Raise(v => v.LoadOrders += _ordersPresenter.OnViewLoad_GetOrders, view, new EventArgs);
_ordersPresenter.AssertWasCalled(d => d.GetOrders); // Getting non-mock exception here
}
How do I Assert GetOrders was called in the SUT? I haven't been able to figure it out in the docs.
Any help is greatly appreciated.
Edit:
I understand the GetOrders method in the SUT should be private. I went back thru Roy Osherove's Art of Unit Testing to see how to test private methods. Roy says making a method public (to test against) is not necessarily a bad thing, so I will keep it public.
So I've written a test for GetOrders and I assert the return value ShouldBe a list of orders. That said, I believe I need to restructure my test for OnViewLoad_GetOrders by stubbing the value I get from GetOrders and asserting the results of my actions on that object.
Can someone confirm and explain?
You can not use AssertWasCalled() on not-mocked objects. Just abstract class OrdersPresenter by an interface (use Extract Interface refactoring technique) and then
var ordersPresenter = MockRepository.GenerateMock<IOrderRepository>();
view.Raise(...);
_ordersPresenter.AssertWasCalled(d => d.GetOrders);
BTW,
for me it is not clear why RhinoMocks not used generic parameter constraint for AssertWasCalled
public static void AssertWasCalled<T>(this T mock, Action<T> action,
Action<IMethodOptions<object>> setupConstraints)
Basically T is not limited, but I believe it would be better limit it to somethign like IMockMarkerInterface

Resources