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

Commit

Permalink
Added TestOption to change xml result file location
Browse files Browse the repository at this point in the history
  • Loading branch information
epsmae committed Oct 12, 2016
1 parent 3542be4 commit a3554fb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
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
25 changes: 25 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,28 @@
// 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 OutputFolderName = "NUnitTestOutput";
const string OutputXmlReportName = "TestResults.xml";

private string _resultFilePath;

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

/// <summary>
/// If True, the tests will run automatically when the app starts
/// otherwise you must run them manually.
Expand All @@ -44,5 +59,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]/NUnitTestOutput/TestResults.xml
/// </summary>
public string ResultFilePath
{
get { return _resultFilePath; }
set { _resultFilePath = value; }
}
}
}
41 changes: 30 additions & 11 deletions src/runner/nunit.runner/Services/XmlFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,43 @@ 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 = new FileSystemFolder(outputFolderName);
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++)
{
IFolder folder = new FileSystemFolder(path);

var res = await folder.CheckExistsAsync(segments[i + 1]);
if (res != ExistenceCheckResult.FolderExists)
{
await folder.CreateFolderAsync(segments[i + 1], CreationCollisionOption.OpenIfExists);
}
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

0 comments on commit a3554fb

Please sign in to comment.