I would like to write some Unit Tests which simulate the following:
1.) User starts my app
2.) User quits my app (unexpectantly during some operation)
3.) User restarts my app
4.) Unit test then checks to see that my code recovered from this.
Having setup UWP Unit tests using these instructions, I can see that the Unit Test project creates a test UWP app like this in UnitTestApp.xaml.cs file:
Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI();
// Ensure the current window is active
Window.Current.Activate();
Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(e.Arguments);
Ideally, I would like to somehow kill/quit/abort this Window (or its Frame, or ??) within a single Unit Test, and then restart it and have this Unit test, and the subsequent Unit tests continue.
If this is not feasible, then I would just like to know how others create unit tests that handle an exit and restart of their application.
I would prefer an answer for Visual Studio 2015, but instructions for Visual Studio 2017 would also be helpful.
It's possible to launch and control the app from test case just like winui did,
UAPApp.Launch would help you launch the application.
I didn't see public doc about UAPApp.Launch, The signature can be found in Microsoft.Windows.Apps.Test.chm
For example, for the navigationview tests, TestEnvironment did the magic:
public static void ClassInitialize(TestContext testContext)
{
TestEnvironment.Initialize(testContext);
}
If you dive deep in TestEnviroment, CreateApplication functions would new Application, and finally in Application.cs, testapp is launched by:
return UAPApp.Launch(_appName, topLevelWindowCondition);
Related
If I create a new Xcode project with Xcode 14 with checked 'Include Tests' checkbox it creates 2 files in the UITests folder:
I am interested in the second one: the [Project]LaunchTests.swift file.
There is this automatically generated code:
func testLaunch() throws {
let app = XCUIApplication()
app.launch()
// Insert steps here to perform after app launch but before taking a screenshot,
// such as logging into a test account or navigating somewhere in the app
let attachment = XCTAttachment(screenshot: app.screenshot())
attachment.name = "Launch Screen"
attachment.lifetime = .keepAlways
add(attachment)
}
If I run this test from the diamond in the code, it runs 4 tests that I can view in the report navigator:
Xcode runs these 4 tests, but I didn't define them anywhere.
Question: where can I find the definition of that tests? Is this kind of an internal testplan which is associated with the LaunchTests file? Where can I find more information about this? It looks like there is a way to run tests with changing light/dark mode and changing orientation without writing a line of code.
Thanks in advance.
If you don't want the four variants of the test to run, then do not (as the template does) return the runsForEachTargetApplicationUIConfiguration value for this test class as true.
As the documentation tells you, when this is true, the test runner consults your actual app target to see what variants it has (light and dark mode, orientations, language localizations).
I am searching for a way to separate the UI tests from the original repository where the main app stays. Is it possible to have the UI test code in another repository, "reference" the main app in some way, and test it?
Starting in Xcode 9, this is possible as long as you're willing to give up a few features. After creating a UI test bundle, you can go to the target settings to specify that it has no target application. (You may also be able to create a UI test bundle without specifying one to begin with.) After you've removed the target application, rather than using the default initializer for XCUIApplication, you can use the initializer that accepts a bundle identifier.
Your tests will end up looking something like the following:
var app: XCUIApplication {
return XCUIApplication(bundleIdentifier: "com.yourdomain.product")
}
func setup() {
app.launch()
}
func test() {
app.navigationBar.buttons["Add"].tap()
}
If you go this route, you will lose the ability to use a few features. Namely:
You won't be able to use Xcode's built in test recorder. The record button is disabled unless you have a target application configured. That being said, you could create an empty app in your project and just switch to the application you'd like to record instead. (That being said, I haven't found Xcode's record feature to be very useful, even for single project apps.)
Xcode won't automatically install your application for you. That usually isn't an issue since is located in a different repository (and sometimes isn't even an Xcode project), so you'll just need to install it before starting your tests. The iOS simulator allows you to drag and drop a .app file, so installing the app is easy enough.
I'm running a UI test in Xcode but the debugger don't stop at any break point in the test code nor in the application code.
It seems like there is no debugging enabled for my test target.
According to Apples doc I should be able to debug my test code, but execution dosent stop at a regular breakpoint.
https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/06-debugging_tests.html#//apple_ref/doc/uid/TP40014132-CH6-SW1
In my schema I have checked the debug flag in the Test-action.
The breakpoint is in the test code. Setting a breakpoint in my unit test code succesfully stops the execusion.
Adding a ui test target to a new project, debugging works fine.
But adding a ui test target to my project don't work.
I'm running out of ideas, anyone that have experienced this too?
We are doing investigation for the Test Clouds for our Android and IOS apps. I made a test snippet which installs and launches the android app. And then takes the screenshot. I am using Visual Studio to write the tests. And Visual Studio emulator to run the android app and xamarin test. Xamarin test is able to install the app, but then throws the error. Code and Errors can be found below.
Other observations:
I am using Test account for my investigations which is free for 30 days.
But I don't see my computer added in the link: https://store.xamarin.com/account/my/subscription/computers. While, I have logged in from Visual Studio in my PC.
Due to this I can't copy the license file too. Can that be the reason? If yes, what is the best way to investigate this without getting full subscription.
Code:
[TestFixture]
public class Tests
{
AndroidApp app;
[SetUp]
public void BeforeEachTest()
{
// TODO: If the Android app being tested is included in the solution then open
// the Unit Tests window, right click Test Apps, select Add App Project
// and select the app projects that should be tested.
app = ConfigureApp
.Android
// TODO: Update this path to point to your Android app and uncomment the
// code if the app is not included in the solution.
.ApkFile(#"C:\MobileOnly\SampleProjects\AndroidSampleProjects\InfraTest\app\build\outputs\apk\app-debug.apk")
// .InstalledApp("com.microsoft.mobile.infratest")
.StartApp(Xamarin.UITest.Configuration.AppDataMode.Clear);
}
[Test]
public void AppLaunches()
{
app.Screenshot("First screen.");
}
}
Error:
Test Name: AppLaunches
Test FullName: XamarinUITest.Tests.AppLaunches
Test Source: : line 0
Test Outcome: Failed
Test Duration: 0:00:11.233
Result StackTrace:
at Xamarin.UITest.Shared.Processes.ProcessRunner.Run(String path, String arguments)
at Xamarin.UITest.Shared.Android.Commands.CommandAdbInstallPackage.Execute(IProcessRunner processRunner, IAndroidSdkTools androidSdkTools)
at Xamarin.UITest.Shared.Android.LocalAndroidAppLifeCycle.InstallApps(ApkFile[] apkFiles)
at Xamarin.UITest.Shared.Android.LocalAndroidAppLifeCycle.EnsureInstalled(ApkFile appApkFile, ApkFile testServerApkFile)
at Xamarin.UITest.Android.AndroidApp..ctor(IAndroidAppConfiguration appConfiguration)
at Xamarin.UITest.Configuration.AndroidAppConfigurator.StartApp(AppDataMode appDataMode)
at XamarinUITest.Tests.BeforeEachTest() in C:\MobileOnly\SampleProjects\AndroidSampleProjects\XamarinUITest\XamarinUITest\Tests.cs:line 22
Result Message:
SetUp : System.Exception : Failed to execute: C:\NugetCache\androidsdk.23.0.4\platform-tools\adb.exe -s 169.254.138.177:5555 install "C:\Users\gunjansa\AppData\Local\Temp\uitest\a-6EAAB1A4CD21F05DB755FBC781EAD620D4ADACBC\final-D9BA1DA5963F9B7853DABC6DEC56BFF2F4740ADE.apk" - exit code: -1073740940
WARNING: linker: libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix.
pkg: /data/local/tmp/final-D9BA1DA5963F9B7853DABC6DEC56BFF2F4740ADE.apk
Update the Xamarin.UITest NuGet and try
public void BeforeEachTest()
{
// TODO: If the Android app being tested is included in the solution then open
// the Unit Tests window, right click Test Apps, select Add App Project
// and select the app projects that should be tested.
app = ConfigureApp
.Android
.StartApp();
}
I would do below actions
Restore your UITest project packages
Make sure that you dont see any app named with the package id though the app is completely uninstalled from the device. Often this is the case where uitests uninstalls the existing version of the app and leaves the folders behind causing this issue.
Hope this resolves your issue.
I'm trying to debug some of my unit tests in Visual Studio 2008 and have noticed that breakpoints don't seem to be halting execution.
I kind of assumed that it was as simple as setting a breakpoint and then doing "Test | Debug | Tests in current context" ... but this never actually hits the breakpoints that I've set.
Am I doing something wrong or is this just broken?
Thanks,
Brandon
I had this same problem until I manually attached to the aspnet_wp.exe process first and then clicked on the Debug Tests buttons. Then my breakpoints were finally hit.
In my case System.Diagnostics.Debugger.Break() doesn't stop at testing method.
[TestClass]
public class ContactListTest
{
#region "Constants"
public const string COVERAGE = "CoverageService";
public const string CompanyList = "CompanyList";
public const string ContactList = "ContactList";
#endregion
[TestMethod]
public void GetContactListTest()
{
System.Diagnostics.Debugger.Break();
var ex = new ServiceFilterExpression(COVERAGE);
ex.Expression = new OpBeginsWith("Type", ContactList);
var result = ex.FetchData();
}
}
if you use nUnit you have to do following
start Nunit with the DLL you want to test.
then in Visual Studio go to
Tools -> Attach to Process
choose your nunit process and click "Attach" then it will halt in all your breakpoints
have fun :-)
The official Microsoft workaround/kludge/zomg-I-can't-believe-they-can't-be-arsed-to-provide-this-after-4-years for MSTEST in VS2010, VS2008, and VS2005 is to add System.Diagnostics.Debugger.Break() to the unit test you want to begin debugging from. This works for all projects with debug symbols referenced by the unit test project.
The .NET runtime will prompt you to dump into debug mode (or close the executing unit test program, or ignore the debug line), and (sometimes) allow you to use instance of visual studio that launched the unit test to do so. You can always debug from a new VS instance. Once you hit that System.Diagnostics.Debugger.Break() line, all other breakpoints will be active and hit (assuming they're in the execution stack).
Check the following:
Are the tests marked with [TestClass] and [TestMethod]?
Are you running Debug or Release mode builds? (Doesn't make a huge difference except when it does) Debug is better.
Are you compiling with or without optimizations? Without is better
Try to run All Tests in Solution in check if you hit the breakpoints
and lastly, maybe you have bug and that's why you are not hitting the code?