xunit / devices.xunit

xUnit.net Runners for Devices
Other
73 stars 36 forks source link

Setting text writer creates 0 byte files #39

Closed kensykora closed 8 years ago

kensykora commented 8 years ago

I think I'm encountering what is a bug, but I'm not really sure where it's happening. I'm trying to setup a file writer for test results on Android 4.4 (Nexus 4)

I have the following as my main activity:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

using Android.App;
using Android.OS;

using Xunit.Runners.UI;
using Xunit.Sdk;

using AndroidEnvironment = Android.OS.Environment;
using Environment = System.Environment;

namespace DeviceGatewaySink.Tests.Android
{
    [Activity(Label = "xUnit Android Runner", MainLauncher = true, Theme = "@android:style/Theme.Material.Light")]
    public class MainActivity : RunnerActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            this.AddTestAssembly(Assembly.GetExecutingAssembly());
            this.AddExecutionAssembly(typeof(ExtensibilityPointFactory).Assembly);
            base.OnCreate(bundle);

            var path =
                AndroidEnvironment.MediaMounted == AndroidEnvironment.ExternalStorageState ?
                    AndroidEnvironment.GetExternalStoragePublicDirectory(AndroidEnvironment.DirectoryDocuments).AbsolutePath
                    : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            path = Path.Combine(path, string.Format("tests-{0}", DateTime.UtcNow.ToString("O").Replace(":", "-") + ".log"));

            Console.WriteLine("Writing log to: {0}", path);
            this.Writer = File.CreateText(path);
        }
    }
}

However, when I do a test run, it creates a file, but the file is 0 bytes -- no content at all inside the file. I tried overriding onpause and closing the file stream but that didn't do anything different.

Any idea what might be going on here? Any other way to get a file with results to get written out?

clairernovotny commented 8 years ago

I just tried this and got it working with one change - this is due to bad documentation.

Make sure to set this.Writer before calling base.OnCreate(). Once the base is called, the properties are not checked again.