Skip to content

Fail of NUnit netcoreapp2 test Project that reference other netcoreapp2.0 lib, with the exception :The type initializer threw an exception. ----> System.ArgumentException : author #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
moh-hassan opened this issue Oct 27, 2018 · 1 comment

Comments

@moh-hassan
Copy link
Collaborator

moh-hassan commented Oct 27, 2018

I have multi-target project (Project1) (net45;netcoreapp2.0) that reference CommandLineParaser library.

Project1 have a static class StartUp with a property:

	private static string _copyRight= CopyrightInfo. Default;

The project is working fine for both frameworks

I have other NUnit Test Project (Project1.Test) which is multi-target project (net45;netcoreapp2.0)

For Test I use dotnet CLI command dotnet test.
I run for both frameworks the following command:

        dotnet test

For net45 framework: Test Run Successful

For netcoreapp2.0: Test Fail with an exception:

Failed DefaultSettingTest()
Error Message:
System.AggregateException : One or more errors occurred. (The type initializerfor 'project1.StartUp' threw an exception.)
----> System.TypeInitializationException : The type initializer for 'project1.StartUp' threw an exception.
----> System.ArgumentException : author
Stack Trace:
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result()
..... other messages-----
--- End of stack trace from previous location where exception was thrown ---
at Project1.StartUp.RunOptionsAsync(String[] args) in F:\project1\Program.cs:line 103
--ArgumentException
at CommandLine.Text.CopyrightInfo..ctor(Boolean isSymbolUpper, String author, Int32[] copyrightYears)
at CommandLine.Text.CopyrightInfo.get_Default()

What is the root problem:

When I investigated CopyrightInfo class,

		  public CopyrightInfo(bool isSymbolUpper, string author, params int[] copyrightYears)
				{
					if (string.IsNullOrWhiteSpace(author)) throw new ArgumentException("author");
					...
				}

I find an exception is fired when AuthorAttribute is null.

Suggestion for a solution:

For The CommandLineParaser library to be independent of the initialization of AssemplyAttributes in Unit Test environment in Full Framework or netcoreapp2.x and avoid cause of test failure as described in this issue, I suggest removing this exception from the line and setting a reasonable value:

 //if (string.IsNullOrWhiteSpace(author)) throw new ArgumentException("author");		
  if (string.IsNullOrWhiteSpace(author)) author="<<UnDefined Author>>";	

Note:
It's better to avoid throwing exception of any Null assemplyAttributes and initialize it with a suitable default value,e.g., CompanyAssemplyAttribute, CopyWrightAssemplyAttributes

@moh-hassan moh-hassan changed the title Fail of NUnit netcoreapp2 Test Project that reference other netcoreapp2.0 lib which depends on CommandLineParaser, with the exception :The type initializer for 'project1.StartUp' threw an exception. ----> System.ArgumentException : author Fail of NUnit netcoreapp2 test Project that reference other netcoreapp2.0 lib using CommandLineParaser, with the exception :The type initializer threw an exception. ----> System.ArgumentException : author Oct 27, 2018
@moh-hassan moh-hassan changed the title Fail of NUnit netcoreapp2 test Project that reference other netcoreapp2.0 lib using CommandLineParaser, with the exception :The type initializer threw an exception. ----> System.ArgumentException : author Fail of NUnit netcoreapp2 test Project that reference other netcoreapp2.0 lib, with the exception :The type initializer threw an exception. ----> System.ArgumentException : author Oct 27, 2018
@moh-hassan
Copy link
Collaborator Author

moh-hassan commented Nov 11, 2018

I have discussed that issue with NUnit team and no solution is available because the behavior of testhost.dll , the details here

The issue can be resolved by injecting Fake AssemplyAttributes before running test.
That can be done if the method ReflectionHelper.SetAttributeOverride() is available for test project.
Currently, it's private and used internally for CommandLine.Test project.
Kindly, can you set the static class ReflectionHelper public in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant