Shortcut keys for running tests in Visual Studio
One of my most used shortcuts in Visual Studio is CTRL+R,T to debug the current test method from the code and CTRL+R,A to execute all tests. Here’s the help topic: How to: Run Selected Tests
To run tests from your test code file, by using the keyboard
1. In Visual Studio, open the source-code file that contains your test methods.
2. Click to define the testing scope: Place the cursor in a test method, in a test class, or outside the scope of a test class.
3. You can use the following keyboard shortcuts to run tests based on that scope.
CTRL + R, then press T This runs the tests in the current scope. That is, it runs the current test method, all the tests in the current test class, or all the tests in the namespace, respectively.
CTRL + R, then press C This runs all the tests in the current test class.
CTRL + R, then press N This runs all tests in the current namespace.
To run tests from source code files in your solution, by using the keyboard
1. In Visual Studio, open a source code file anywhere in your solution.
2. You can use the following keyboard shortcuts to run tests from that file.
Ctrl + R, then press A Runs all the tests in all test projects.
Ctrl + R, then press D Runs all tests that were run in the last test run.
Ctrl + R, then press F Runs all tests in the last test run that did not pass.
HowTo update appsettings programmatically
Recently I was writing functional test for an application that read settings from the appsettings element in App.Config. The problem was that the application under test was expecting to react different based on the settings so need a way to alter the values between tests. I ended up writing bellow code for it
private static void SetAppSettings(string key, string value) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings[key].Value = value; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } |
How to isolate Directory.Exists in unit testing using disposable pattern
Earlier i wrote a blog post about isolating DateTime.Now with disposable pattern. Here is a little more advance example for the static method Directory.Exists.
So here is the simple example of product code that we want to test
public void ProcessFiles(string path) { if (!DirectoryIsolator.Exists(path)) { throw new ArgumentException("Path doesn't exist"); } } |
This first test method is simple setting the return value to false before calling the product code
[TestMethod()] [ExpectedException(typeof(ArgumentException))] public void Class1_ProcessFiles_SettingReturnValueFalse() { using (DirectoryIsolator.ExistsIs(false)) { Class1 target = new Class1(); string path = @"C:\windows"; target.ProcessFiles(path); } } |
How to isolate DateTime.Now in unit testing
When writing unit tests for codes that is using DateTime.Now you will find yourself in a situation where you can’t automate functionality that are dependent on the time since the time is changing constantly. You are also not able to test for example that your codes work correctly for leap year or change of year, etc.
How would you test correctness of bellow simple example?
public bool IsIt2012 { get { return (DatemTime.Now.Year == 2012); } } |
The solution is to introduce a “wrapper” for DateTime.
public bool IsIt2012 { get { return (DatemTimeIsolator.Now.Year == 2012); } } |
The TestApi v0.4 has shipped
The TestApi v0.4 has shipped! You can download the latest bits at http://codeplex.com/testapi. Some of the updates
• An improved Combinatorial Variation Generation API – we now support parameter value weights and tags (for “negative” variations);
• A new Memory Leak Detection API – allowing capture, comparison, and serialization/de-serialization of memory snapshots of a running process;
• A new Object Comparison API – allowing comparison of arbitrary .NET objects using arbitrary object comparison strategies;
• A new Text String Generation API – allowing generation of random strings, interesting from the testing point of view;
• Various documentation improvements and sample additions.
WPF Application Quality Guide v0.5 has been released
WPF Application Quality Guide v0.5 has been released today at the WindowsClient.NET with new topics and updates.
This release includes several new additions and updates:
1. Suggested Roadmap – updated to include new topics based on different persona
2. Performance and Scalability Testing – updated with a few new resources
3. Integration and Scenario Testing – a new article outlines the strategies and the steps to take in Integration and Scenario testing.
4. TestApi (Tools) – updated with summary of the new APIs from TetsApi v0.2 as well as sample usages.
5. Tools – various new additions and updates to commonly used tools.
6. A1 Building a WPF Application Test Suite by Using VSTS, NUnit, or xUnit – a new article summarized the common unit testing frameworks including sample test code and key resources.
7. A4 Considerations for WPF Browser Applications – a new valuable write-up with common considerations in XBap apps.
UI automation with accessibility object and win 32 API
In my earlier blogs (Fun with UIAutomation and calc, Fun with UIAutomation and calc 2 (event handling)) I have been talking about how to user uiautomation in .NET. Now it time to dig down bellow uiautomation and go directly to the source WIN 32 API. I have tried to translate the first calculator project to use win 32 API and accessibility object instead. The first thing that we need to-do is to create DllImport statement for user32.dll used for handling more or less all windows UI and oleacc.dll used for accessing the accessibility object. You will find my complete solution in the end of the blog.
An example of DllImport is EnumChildWindows that gives use the possibility to get all child windows to a specific window.
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam); |
How to determine if we are running on 64 or 32 bit?
In my work I need to be able to pick different files depending on if I run in 64 bit or 32 bit system. The first thing I did try was to use the size of IntPtr which is 4 for 32 bit and 8 for 64 bit. But since I was using the visual studio unit test framework it did not work that god. The unit test framework run in a 32 bit process but the application I’m testing is running in 64 bit, so decided to use P/Invoke to call GetNativeSystemInfo.
The structure that you get is according to
[StructLayout(LayoutKind.Sequential)] private struct SystemInfoNative { internal ushort ProcessorArchitecture; internal ushort Reserved; internal uint PageSize; internal IntPtr MinimumApplicationAddress; internal IntPtr MaximumApplicationAddress; internal IntPtr ActiveProcessorMask; internal uint NumberOfProcessors; internal uint ProcessorType; internal uint AllocationGranularity; internal ushort ProcessorLevel; internal ushort ProcessorRevision; } |
Out from this you can check the processor architecture.
And the complete source code
Read more
Introducing WPF Application Quality Guide v.0.4
WPF Application Quality Guide v0.4 has been released today at the WindowsClient.NET with new topics and updates.
This release includes several new topics and updates:
1. Suggested Roadmap – updated to include new topics
2. Data-Driven Testing – a new article about the data-driven testing practice.
3. Globalization and Localization Testing – updated w/ more details including creating localizable UI layouts in WPF and approaches to localizing WPF applications.
4. Stability and Stress Testing – a new article about stress testing principles, best practices, and useful resources.
5. Accessibility Testing – a new article about accessibility considerations, best practices and key resources.
6. Tools / TestAPI – a brief intro about the newly released TestAPIs.
The full online version can be found here.
Fun with UIAutomation and calc
Last night I got a cool idea how to show some basic ideas of UIAutomation. I did decide to-do a UIAutomation example with calc as my test subject. The idea is to give a mathematical expression to the program, which will then use calc to solve the problem and show you the result. The main thing is to show how UIAutomation is working and not to calculate.
The first thing that we need is a method that can find an AutomationElement for us. An AutomationElement is anything that you can see on the screen as buttons and input fields. The method take a root AutomationElement that define where to start to search, a name and a class name for the object that we are looking for. We then create two property conditions based on the name and class name. These two conditions are then combined in an AndCondition. We also have OrCondition and NotCondition to use. The last thing we do is to search for the first AutomationElement that match the condition.
private static AutomationElement GetElement(AutomationElement root, string name, string className) { PropertyCondition condName = new PropertyCondition(AutomationElement.NameProperty, name); PropertyCondition condClassName = new PropertyCondition(AutomationElement.ClassNameProperty, className); AndCondition cond = new AndCondition(condName, condClassName); return root.FindFirst(TreeScope.Descendants, cond); } |
Next method that we need is one that can return a invoke pattern. Invoke pattern is used for example to click on a button. This will be used to be able to click on the different buttons in calc
private static InvokePattern GetInvokePattern(AutomationElement element) { return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern; } |