Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #60 from epsmae/pull-request-result-filepath-option
Browse files Browse the repository at this point in the history
Added TestOption to change xml result file location
  • Loading branch information
ChrisMaddock committed Nov 6, 2016
2 parents c415c87 + ddafa52 commit 0fa532e
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 71 deletions.
3 changes: 3 additions & 0 deletions nunit.runner.sln
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\runner\nunit.runner\nunit.runner.projitems*{09bcb5fa-30ad-49af-8034-5670ccb320b5}*SharedItemsImports = 4
src\tests\nunit.runner.tests\nunit.runner.tests.projitems*{17a2b97c-56ea-40b8-8901-5e0728876657}*SharedItemsImports = 4
src\tests\nunit.runner.tests\nunit.runner.tests.projitems*{2a198895-edfb-4a82-899e-e7d1d1c592e8}*SharedItemsImports = 4
src\tests\nunit.runner.tests\nunit.runner.tests.projitems*{391ce92c-08f6-4ddf-941c-a8980dcc196e}*SharedItemsImports = 4
src\runner\nunit.runner\nunit.runner.projitems*{3bf3745a-8491-46a9-af66-f8de22accf31}*SharedItemsImports = 4
src\runner\nunit.runner\nunit.runner.projitems*{47b8b14f-da28-4188-becb-23c4ee51e0f5}*SharedItemsImports = 4
src\tests\nunit.runner.tests\nunit.runner.tests.projitems*{af293652-baa0-4813-b4f3-2d5a2cdde0b4}*SharedItemsImports = 13
src\tests\nunit.runner.tests\nunit.runner.tests.projitems*{b0040786-1ab0-4fd6-93d2-e5ba330fb4bc}*SharedItemsImports = 4
src\runner\nunit.runner\nunit.runner.projitems*{c9209134-aea3-4c7f-ad74-00d578a6f208}*SharedItemsImports = 13
Expand Down Expand Up @@ -213,6 +215,7 @@ Global
{391CE92C-08F6-4DDF-941C-A8980DCC196E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{391CE92C-08F6-4DDF-941C-A8980DCC196E}.Debug|ARM.ActiveCfg = Debug|Any CPU
{391CE92C-08F6-4DDF-941C-A8980DCC196E}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{391CE92C-08F6-4DDF-941C-A8980DCC196E}.Debug|iPhone.Deploy.0 = Debug|Any CPU
{391CE92C-08F6-4DDF-941C-A8980DCC196E}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{391CE92C-08F6-4DDF-941C-A8980DCC196E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{391CE92C-08F6-4DDF-941C-A8980DCC196E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
Expand Down
2 changes: 1 addition & 1 deletion src/runner/nunit.runner.Droid/nunit.runner.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<DevInstrumentationEnabled>True</DevInstrumentationEnabled>
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
Expand Down
24 changes: 24 additions & 0 deletions src/runner/nunit.runner/Services/TestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,27 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using PCLStorage;

namespace NUnit.Runner.Services
{
/// <summary>
/// Options for the device test suite.
/// </summary>
public class TestOptions
{
const string OutputXmlReportName = "TestResults.xml";

private string _resultFilePath;

/// <summary>
/// Constructor
/// </summary>
public TestOptions()
{
_resultFilePath = System.IO.Path.Combine(FileSystem.Current.LocalStorage.Path, OutputXmlReportName);
}

/// <summary>
/// If True, the tests will run automatically when the app starts
/// otherwise you must run them manually.
Expand All @@ -44,5 +58,15 @@ public class TestOptions
/// Creates a NUnit Xml result file on the host file system using PCLStorage library.
/// </summary>
public bool CreateXmlResultFile { get; set; }

/// <summary>
/// File path for the xml result file
/// Default is [LocalStorage]/TestResults.xml
/// </summary>
public string ResultFilePath
{
get { return _resultFilePath; }
set { _resultFilePath = value; }
}
}
}
63 changes: 47 additions & 16 deletions src/runner/nunit.runner/Services/XmlFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using NUnit.Framework.Interfaces;

using PCLStorage;

using CreationCollisionOption = PCLStorage.CreationCollisionOption;
using FileAccess = PCLStorage.FileAccess;

namespace NUnit.Runner.Services
{
class XmlFileProcessor : TestResultProcessor
{
public XmlFileProcessor(TestOptions options)
: base(options)
{
}
: base(options) { }

public override async Task Process(ITestResult testResult)
{
Expand All @@ -64,24 +62,57 @@ public override async Task Process(ITestResult testResult)

async Task WriteXmlResultFile(ITestResult testResult)
{
const string OutputFolderName = "NUnitTestOutput";
const string OutputXmlReportName = "TestResults.xml";
var localStorageFolder = FileSystem.Current.LocalStorage;
string outputFolderName = Path.GetDirectoryName(Options.ResultFilePath);
string outputXmlReportName = Path.GetFileName(Options.ResultFilePath);

var existResult = await localStorageFolder.CheckExistsAsync(OutputFolderName);
if (existResult == ExistenceCheckResult.FileExists)
{
var existingFile = await localStorageFolder.GetFileAsync(OutputFolderName);
await existingFile.DeleteAsync();
}
await CreateFolderRecursive(outputFolderName);

var outputFolder = await localStorageFolder.CreateFolderAsync(OutputFolderName, CreationCollisionOption.OpenIfExists);
IFile xmlResultFile = await outputFolder.CreateFileAsync(OutputXmlReportName, CreationCollisionOption.ReplaceExisting);
IFolder outputFolder =
await FileSystem.Current.GetFolderFromPathAsync(outputFolderName, CancellationToken.None);

IFile xmlResultFile =
await outputFolder.CreateFileAsync(outputXmlReportName, CreationCollisionOption.ReplaceExisting);
using (var resultFileStream = new StreamWriter(await xmlResultFile.OpenAsync(FileAccess.ReadAndWrite)))
{
string xmlString = testResult.ToXml(true).OuterXml;
await resultFileStream.WriteAsync(xmlString);
}
}

/// <summary>
/// Create a folder from the full folder path if it does not exist
/// Throws exception if acess to the path is denied
/// </summary>
/// <param name="folderPath"></param>
/// <returns></returns>
private static async Task CreateFolderRecursive(string folderPath)
{
string[] segments = new Uri(folderPath).Segments;

string path = segments[0];

for (int i = 0; i < segments.Length - 1; i++)
{
try
{
#if __DROID__ || __IOS__
IFolder folder = await FileSystem.Current.GetFolderFromPathAsync(path, CancellationToken.None);
#else
IFolder folder = await FileSystem.Current.GetFolderFromPathAsync(path.Replace('/', '\\'), CancellationToken.None);
#endif
var res = await folder.CheckExistsAsync(segments[i + 1]);
if (res != ExistenceCheckResult.FolderExists)
{
await folder.CreateFolderAsync(segments[i + 1], CreationCollisionOption.OpenIfExists);
}
}
catch (Exception)
{
// ignore
}

path = Path.Combine(path, segments[i + 1]);
}
}
}
}
6 changes: 5 additions & 1 deletion src/tests/nunit.runner.tests.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System.IO;
using Android.App;
using Android.Content.PM;
using Android.OS;
Expand Down Expand Up @@ -56,7 +57,10 @@ protected override void OnCreate(Bundle savedInstanceState)
//TcpWriterParameters = new TcpWriterInfo("192.168.0.108", 13000),

// Creates a NUnit Xml result file on the host file system using PCLStorage library.
CreateXmlResultFile = true
CreateXmlResultFile = true,

// Choose a diffrent path for the xml result file
ResultFilePath = Path.Combine(Environment.ExternalStorageDirectory.Path, Environment.DirectoryDownloads, "Nunit", "Results.xml")
};

LoadApplication(nunit);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="nunit.runner.tests" android:versionCode="3" android:versionName="3.0">
<uses-sdk android:minSdkVersion="15" />
<application android:label="NUnit" android:icon="@drawable/icon"></application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="NUnit"></application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
7 changes: 6 additions & 1 deletion src/tests/nunit.runner.tests.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.IO;
using Foundation;

using NUnit.Runner.Services;
Expand Down Expand Up @@ -64,7 +66,10 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary options)
//TcpWriterParameters = new TcpWriterInfo("192.168.0.108", 13000),

// Creates a NUnit Xml result file on the host file system using PCLStorage library.
CreateXmlResultFile = false
CreateXmlResultFile = true,

// Choose a diffrent path for the xml result file (ios file share / library directory)
ResultFilePath = Path.Combine(NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User)[0].Path, "Results.xml")
};

LoadApplication(nunit);
Expand Down
102 changes: 53 additions & 49 deletions src/tests/nunit.runner.tests.iOS/Info.plist
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
<integer>2</integer>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>MinimumOSVersion</key>
<string>6.0</string>
<key>CFBundleDisplayName</key>
<string>nunit.runner</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.nunit.runner</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>CFBundleIconFiles</key>
<array>
<string>Icon-60@2x</string>
<string>Icon-60@3x</string>
<string>Icon-76</string>
<string>Icon-76@2x</string>
<string>Default</string>
<string>Default@2x</string>
<string>Default-568h@2x</string>
<string>Default-Portrait</string>
<string>Default-Portrait@2x</string>
<string>Icon-Small-40</string>
<string>Icon-Small-40@2x</string>
<string>Icon-Small-40@3x</string>
<string>Icon-Small</string>
<string>Icon-Small@2x</string>
<string>Icon-Small@3x</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
</dict>
<dict>
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
<integer>2</integer>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>MinimumOSVersion</key>
<string>6.0</string>
<key>CFBundleDisplayName</key>
<string>nunit.runner</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.nunit.runner</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>CFBundleIconFiles</key>
<array>
<string>Icon-60@2x</string>
<string>Icon-60@3x</string>
<string>Icon-76</string>
<string>Icon-76@2x</string>
<string>Default</string>
<string>Default@2x</string>
<string>Default-568h@2x</string>
<string>Default-Portrait</string>
<string>Default-Portrait@2x</string>
<string>Icon-Small-40</string>
<string>Icon-Small-40@2x</string>
<string>Icon-Small-40@3x</string>
<string>Icon-Small</string>
<string>Icon-Small@2x</string>
<string>Icon-Small@3x</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>CFBundleShortVersionString</key>
<string></string>
<key>UIFileSharingEnabled</key>
<true/>
</dict>
</plist>
6 changes: 5 additions & 1 deletion src/tests/nunit.runner.tests.uwp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public MainPage()
//TcpWriterParameters = new TcpWriterInfo("192.168.0.108", 13000),

// Creates a NUnit Xml result file on the host file system using PCLStorage library.
CreateXmlResultFile = false
CreateXmlResultFile = false,

// Choose a diffrent path for the xml result file
// Windows phone 8.1 no permission to create subfolder in Windows.Storage.ApplicationData.Current.LocalFolder
// ResultFilePath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.TemporaryFolder.Path, "Nunit", "Results.xml")
};

LoadApplication(nunit);
Expand Down
1 change: 1 addition & 0 deletions src/tests/nunit.runner.tests.uwp/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
<Capabilities>
<Capability Name="internetClient" />
<Capability Name="privateNetworkClientServer" />
<uap:Capability Name="removableStorage" />
</Capabilities>
</Package>

0 comments on commit 0fa532e

Please sign in to comment.