visualstudio.xunit icon indicating copy to clipboard operation
visualstudio.xunit copied to clipboard

The active test run was aborted. Reason: Unexpected character encountered

Open quixoticaxis opened this issue 5 years ago • 0 comments

If I pass strings that are not correct Unicode as parameters to theories, the test run fails:

The active test run was aborted. Reason: Unexpected character encountered while parsing value

If I use dotnet test --diag log.log, I see that the values cannot be deserialized by Newtonsoft.Json. I'm not certain whether this is a bug of xunit runner, or of the runner infrastructure of vstest.

TpTrace Verbose: 0 : 18612, 10, 2020/10/22, 16:31:21.906, 155947063999, vstest.console.dll, TestRequestSender: GetAbortErrorMessage: Exception: Newtonsoft.Json.JsonReaderException: Bad JSON escape sequence: \�. Path 'Payload.NewTestResults[1].DisplayName', line 1, position 1896. at Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer(Char quote) at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonWriter.WriteToken(JsonReader reader, Boolean writeChildren, Boolean writeDateConstructorAsDate, Boolean writeComments) at Newtonsoft.Json.Linq.JTokenWriter.WriteToken(JsonReader reader, Boolean writeChildren, Boolean writeDateConstructorAsDate, Boolean writeComments) at Newtonsoft.Json.JsonWriter.WriteToken(JsonReader reader, Boolean writeChildren) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateJToken(JsonReader reader, JsonContract contract) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader) at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.JsonDataSerializer.Deserialize[T](JsonSerializer serializer, String data) at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.JsonDataSerializer.DeserializeMessage(String rawMessage) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyExecutionManager.HandleRawMessage(String rawMessage) at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TestRequestSender.OnExecutionMessageReceived(Object sender, MessageReceivedEventArgs messageReceived, ITestRunEventsHandler testRunEventsHandler)

The code to reproduce:

using System;
using System.Linq;
using System.Text;
using Xunit;

namespace XUnitRunner
{
    public class UnitTest1
    {
        [Theory]
        [MemberData(nameof(GetRandomStrings))]
        public void Test1(string @string)
        {
            Assert.Equal(@string, "a");
        }

        public static TheoryData<string> GetRandomStrings()
        {
            var data = new TheoryData<string>();

            var generator = new Random();

            char PickRandomCharacter()
                => randomCharacters[generator.Next(0, randomCharacters.Length)];

            foreach(var i in Enumerable.Range(0, 100))
            {
                var randomString = Enumerable.Range(0, 100)
                    .Aggregate(
                        new StringBuilder(),
                        (builder, character) => builder.Append(
                            PickRandomCharacter()))
                    .ToString();

                data.Add(randomString);
            }

            return data;
        }

        private static readonly char[] randomCharacters
            = Enumerable.Range(char.MinValue, char.MaxValue + 1)
                .Select(characterCode => Convert.ToChar(characterCode))
                .Where(c => !char.IsLetterOrDigit(c))
                .ToArray();
    }
}

quixoticaxis avatar Oct 22 '20 13:10 quixoticaxis