Check parameters needed by external program - c#

I need to run external program to automatize task and I need to know what parameters I need to pass.
Do I have to decompile external software or is there any easier and more likely to success way to check what data I need to pass?

Related

Selenium C#, Runing multiple test with predefined parameters

I am looking for a way to predefine some parameters (URL's) and then run a selenium test for each of them in C#.
Where should I specify those URL's?
How to implement reading of those parameters in my code?
[Test]
public void Test(string URLParameter)
{
driver.Navigate().GoToUrl(URLParameter);
}
And how should I run those tests?
If you are looking for a way to run the same test with a different set of test data i.e: a different URL in your case and If you are using
Either Nunit or xUnit
Please note all built-in attributes allow for data to be provided programmatically, but NUnit does not have any attributes that fetch the data from a file or other external source.
You can make use of
[TestCaseSource]
attribute
Please read more about it from Nunit Documentation
References:
Parameterized-Tests
Custom-Attributes
There are several ways to predefine and keep such test data.
You can keep it in some external file. It can be JSON, XML or plain text formatted file.
It may be kept in some resource C# class file inside the project.
It can be kept in some kind of DB etc.
Actual implementation of how to read this data will vary according to the way you decided to keep the test data, according to your project structure etc.
There are several best practices how to do that, not only one way to do that.
There are also several ways to run such tests. You can learn about these best practices on many online tutorials and other resources.
For that purpose you should use multiple different ways to keep test data.
1- Kept in the resources folder of the project for example in
properties file.
2- Kept data in DB and read from there while
needed.
3- Kept in some external sources and read/retrieve if from
there.
Note: Should use that method which will be convenient for you later on.

Identifying non used functions through unit test

I have written a helper class which has many public functions in it.
Few of the functions I have called through different files in the same project.
Few function I am not calling as I thinking I use it for future.
Few function I may be written mistakenly and may not be useful in future or it may be a total waste.
Question:
Is it possible to write test cases to check which functions are called and which are NOT called?
Can we write test cases to make the test fail if the helper function is not used?
Is it a best practice to keep the unused functions in help class?
Update :
If I keep unused functions in my class for future reference and write
unit tests for that, test coverage includes those functions and gives
wrong test coverage, so is it right to write test cases for those
functions or is it better to remove those unused functions?
In short, probably not as it would rely on you having 100% coverage of the rest of your system and using some kind of code coverage tool. Something like Resharper code inspection will tell you which of the methods it doesn't think are being used.

Console App to start a CodedUI test

I have a coded ui test that I want to start by using batch, its a .dll file.
The reason I'm calling it from a batch file is that the server has to be restarted before this test can be carried out.
Is it just a simple call test.dll or do I have to do other stuff?
Update Code Found
Playback.Initialize();
TestProject.CodedUITest1 coded = new TestProject.CodedUITest1();
coded.CodedUITestMethod1();
Playback.Cleanup();
Take from Here, Its missing two reference addings from the private assemblies:
Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.dll
Microsoft.VisualStudio.TestTools.UITest.Extension.IE.dll
Hope this helps other people looking to do this
You can't run a Dll file like you can an exe. A Dll contains code intended to be used by a program, it means one source of code can be used by many programs, which saves duplicating the code.
Usually the Dll will have documented functions you can call via a program, unless you built it yourself in which case you would know :)
Try and find the program that uses the Dll and call that, or find the docs for it and run the function from rundll32 as #PA. suggested.
DLLs are dynamic libraries that need to be linked and called from an application program. Every DLL has its own exported interface, or collection of entry points to be called from the external executable, or, may be, from another DLL.
Windows provides a set of calls to help caller programs to load, detect entry points, and unload DLLs. Beyond this limited common functionality, there are endless combinations of ways of using a DLL, in the calling conventions, in the ways of passing parameters, in the types of the parameters, in the ways of returning data, in the ways of synchronizing, notifying events, interrupting, multithreading, in almost every aspect of programming models.
Having said so, it is possible that your DLL is expected to be called from some specific application program, and thus is possibly following and strict and well defined API. One such type of DLLs are Windows System DLLs that are intended to be run with rundll executable program. rundll32.exe is the Windows system executable that launches and invokes functions that are packed and shipped in .dll files, from a DLL that is explicitely programmed to be called this way.
to invoke your TestFunction inside your TEST.DLL, passing 1234 as a parameter, you'd use
RUNDLL32 TEST.DLL, TestFunction 1234
Rundll will perform for you the following tasks
Load TEST.DLL via LoadLibrary().
Address the TestFunction function via GetProcAddress().
Call TestFunction function, passing the rest of the command line
Unload the DLL and exit.

unit test for exporting XML method

I have a method ExportXMLFiles(string path) to export xml files at a certain path with some elements inside it like FirstName, LastName, MajorSubject. These values are getting picked from a database.
Now I need to write a Unit test method for it and I have not worked on much unit tests except simple and straight forward ones. My confusion is, do I need to connect to database and create a XML file or do I need to pass hard coded values while creating XML file so that I can validate the values in XML created?
Is there any other way for doing this?
You absolutely do not want to use an actual database in your unit test. It adds one level of complexity that you don't want to deal with in your unit tests. It also makes your unit tests less reliable and slower. See if you can break the database functionality into an interface that you can instantiate using a mocking framework. Try looking into something like moq or if that isn't enough check out moles from Microsoft .
Edit - Another post mentioned that if the functionality is to write to the disk then your unit test should validate that the file was written out to disk. Using Moles you can simulate file systems and test your file system calls and simulate write failures or whatever other cases you need which would give you a lot more flexibility and speed than actually physically writing to disk. Things like a disk write failing would be miserable to test without something like moles.
A unit test should be small in scope and isolated from dependencies eg databases and file systems. So what you want to do is look at mocking out the database access and what would get written to a file so that you can run your test without needing particular values in the database. Unit tests should be fast to run, have repeatable results (ie run twice, get the same answer), isolated from other tests and able to be run in any order.
A unit test is looking at ONE item of functionality and not relying on the behaviour of anything else.
So look at using a pattern such as dependency injection so you can provide (ie inject) database and file system dependencies. Look at a mocking framework such as NMock or write your own lightweight fake objects that implement the same interface as the dependencies and then you can pass those into your functions being tested.
What is the responsibility of this method ?
Is it to dump given data in the form of xml files at a certain path? If yes, then you'd have to check that the files are in fact created.
This is not a unit test but an integration test (because this is the class at the boundary between your app and the filesystem). You should abstract away the input data source (the DB) via an interface/role. You can also create a Role to CreateXmlFile(contents) but I think that's overkill.
// setup mock data source to supply canned data
// call myObject.ExportToXml(mockDataSource, tempPath)
// verify files are created in tempPath
Finally this class needs to implement a Role (DataExporter) so that the tests that use DataExporter are fast / don't have to deal with filesystems (or XML).

Is it possible to virtualize a console application's disk access when shelled out from a C# application

I've got application A which runs application B as a console app. I'd like to write unit tests as part of application A which validate the input to/output from application B, but application B uses hard coded paths to look for some of its inputs. I'd like to be able to run the application, but intercept the call to read from c:\wherever\whatever.txt and provide the contents of that file myself.
Any frameworks or pieces which can do this for me?
This requires patching the Win32 CreateFile API function. Which is merely technically possible with Detours from Microsoft Research. Which requires unmanaged C or C++.
Tackle this problem at the source, having hard-coded path names in source code is unreasonable.
This is almost impossible!! I say almost because it is possible to intercept calls in the CLR but it is black magic and not recommended unless you are a CLR developer - perhaps a few hundred people in the world.
You can make the hard-coded paths as command line parameters and solve your problem this way.
You could include application B as a reference in application A, and then use Mocking frameworks to change the behaviour of B's methods or properties when calling it's API directly. This is the same process used for unit tests when setting up expectations on dependencies. There are limitations that generally this only works if the object in question is an interface, or contains vi
rtual (overridable) methods/properties. The solution may also be dependent on being able inject a dependency into Bs API, which may or may not be possible depending on the scenario.
Moq, Rhino Mocks. and TypeMock all provide this functionality. Here's a quick example of Moq to override the behaviour of a GetPath method with an alternative value:
// create a mocked version of a class and setup an expectation
var appBClassMoq = new Mock<AppBClass>();
appBClassMoq.SetUp(o => o.GetPath()).Returns("C:\MyNewPath");
// get the mocked instance
var appBClass = appBClassMoq.Object;
// run some code, when it hits GetPath() it will return the mock value
appBClass.SomeMethodThatCallsGetPath();

Categories

Resources