Skip to content
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

Feature: add support for injecting TestContext in ctor #3267

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ public class TestClassInfo
internal TestClassInfo(
Type type,
ConstructorInfo constructor,
bool isParameterlessConstructor,
PropertyInfo? testContextProperty,
TestClassAttribute classAttribute,
TestAssemblyInfo parent)
{
DebugEx.Assert(type != null, "Type should not be null");
DebugEx.Assert(constructor != null, "Constructor should not be null");
DebugEx.Assert(parent != null, "Parent should not be null");
DebugEx.Assert(classAttribute != null, "ClassAttribute should not be null");

ClassType = type;
Constructor = constructor;
IsParameterlessConstructor = isParameterlessConstructor;
TestContextProperty = testContextProperty;
BaseClassCleanupMethodsStack = new Stack<MethodInfo>();
BaseClassInitAndCleanupMethods = new Queue<Tuple<MethodInfo?, MethodInfo?>>();
Expand Down Expand Up @@ -77,6 +74,8 @@ internal TestClassInfo(
/// </summary>
public ConstructorInfo Constructor { get; }

internal bool IsParameterlessConstructor { get; }

/// <summary>
/// Gets the test context property.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ private bool SetTestContext(object classInstance, TestResult result)
object? classInstance = null;
try
{
classInstance = Parent.Constructor.Invoke(null);
classInstance = Parent.Constructor.Invoke(Parent.IsParameterlessConstructor ? null : [TestMethodOptions.TestContext]);
}
catch (Exception ex)
{
Expand Down
20 changes: 15 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,22 @@ private static bool TryGetUnescapedManagedTypeName(TestMethod testMethod, [NotNu
/// <returns> The <see cref="TestClassInfo"/>. </returns>
private TestClassInfo CreateClassInfo(Type classType, TestMethod testMethod)
{
ConstructorInfo? constructor = classType.GetConstructor([]);
bool isParameterLessConstructor;
ConstructorInfo constructor;

if (constructor == null)
if (classType.GetConstructor([typeof(TestContext)]) is { } testContextCtor)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_NoDefaultConstructor, testMethod.FullClassName);
throw new TypeInspectionException(message);
constructor = testContextCtor;
isParameterLessConstructor = false;
}
else if (classType.GetConstructor([]) is { } parameterLessCtor)
{
constructor = parameterLessCtor;
isParameterLessConstructor = true;
}
else
{
throw new TypeInspectionException(string.Format(CultureInfo.CurrentCulture, Resource.UTA_NoValidConstructor, testMethod.FullClassName));
}

PropertyInfo? testContextProperty = ResolveTestContext(classType);
Expand All @@ -283,7 +293,7 @@ private TestClassInfo CreateClassInfo(Type classType, TestMethod testMethod)

TestClassAttribute? testClassAttribute = ReflectHelper.Instance.GetFirstDerivedAttributeOrDefault<TestClassAttribute>(classType, inherit: false);
DebugEx.Assert(testClassAttribute is not null, "testClassAttribute is null");
var classInfo = new TestClassInfo(classType, constructor, testContextProperty, testClassAttribute, assemblyInfo);
var classInfo = new TestClassInfo(classType, constructor, isParameterLessConstructor, testContextProperty, testClassAttribute, assemblyInfo);

// List holding the instance of the initialize/cleanup methods
// to be passed into the tuples' queue when updating the class info.
Expand Down
15 changes: 7 additions & 8 deletions src/Adapter/MSTest.TestAdapter/Resources/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Adapter/MSTest.TestAdapter/Resources/Resource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ Error: {1}</value>
<data name="UTA_MethodDoesNotExists" xml:space="preserve">
<value>Method {0}.{1} does not exist.</value>
</data>
<data name="UTA_NoDefaultConstructor" xml:space="preserve">
<value>Unable to get default constructor for class {0}.</value>
<data name="UTA_NoValidConstructor" xml:space="preserve">
<value>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</value>
</data>
<data name="UTA_TestContextLoadError" xml:space="preserve">
<value>Unable to find property {0}.TestContext. Error:{1}.</value>
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Resources/xlf/Resource.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ byl však přijat tento počet argumentů: {4} s typy {5}.</target>
<target state="translated">Nepodařilo se vytvořit instanci třídy {0}. Chyba: {1}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoValidConstructor">
<source>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</source>
<target state="new">Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</target>
<note />
</trans-unit>
<trans-unit id="UTA_TestContextSetError">
<source>Unable to set TestContext property for the class {0}. Error: {1}.</source>
<target state="translated">Pro třídu {0} se nepodařilo nastavit vlastnost TestContext. Chyba: {1}.</target>
Expand Down Expand Up @@ -263,11 +268,6 @@ Chyba: {1}</target>
<target state="translated">Metoda {0}.{1} neexistuje.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoDefaultConstructor">
<source>Unable to get default constructor for class {0}.</source>
<target state="translated">Pro třídu {0} se nepodařilo najít výchozí konstruktor.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_TestContextLoadError">
<source>Unable to find property {0}.TestContext. Error:{1}.</source>
<target state="translated">Nepodařilo se najít vlastnost {0}.TestContext. Chyba:{1}.</target>
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Resources/xlf/Resource.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ aber empfing {4} Argument(e) mit den Typen „{5}“.</target>
<target state="translated">Es kann keine Instanz der Klasse '{0}' erstellt werden. Fehler: {1}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoValidConstructor">
<source>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</source>
<target state="new">Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</target>
<note />
</trans-unit>
<trans-unit id="UTA_TestContextSetError">
<source>Unable to set TestContext property for the class {0}. Error: {1}.</source>
<target state="translated">Die Eigenschaft 'TestContext' für die Klasse '{0}' kann nicht festgelegt werden. Fehler: {1}.</target>
Expand Down Expand Up @@ -263,11 +268,6 @@ Fehler: {1}</target>
<target state="translated">Die Methode "{0}.{1}" ist nicht vorhanden.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoDefaultConstructor">
<source>Unable to get default constructor for class {0}.</source>
<target state="translated">Der Standardkonstruktor für die Klasse "{0}" kann nicht abgerufen werden.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_TestContextLoadError">
<source>Unable to find property {0}.TestContext. Error:{1}.</source>
<target state="translated">Die Eigenschaft "{0}.TestContext" wurde nicht gefunden. Fehler: {1}.</target>
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Resources/xlf/Resource.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ pero recibió {4} argumento(s), con los tipos "{5}".</target>
<target state="translated">No se puede crear una instancia de la clase {0}. Error: {1}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoValidConstructor">
<source>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</source>
<target state="new">Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</target>
<note />
</trans-unit>
<trans-unit id="UTA_TestContextSetError">
<source>Unable to set TestContext property for the class {0}. Error: {1}.</source>
<target state="translated">No se puede establecer la propiedad TestContext para la clase {0}. Error: {1}.</target>
Expand Down Expand Up @@ -263,11 +268,6 @@ Error: {1}</target>
<target state="translated">El método {0}.{1} no existe.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoDefaultConstructor">
<source>Unable to get default constructor for class {0}.</source>
<target state="translated">No se puede obtener el constructor predeterminado para la clase {0}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_TestContextLoadError">
<source>Unable to find property {0}.TestContext. Error:{1}.</source>
<target state="translated">No se puede encontrar la propiedad {0}.TestContext. Error:{1}.</target>
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Resources/xlf/Resource.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ mais a reçu {4} argument(s), avec les types « {5} ».</target>
<target state="translated">Impossible de créer une instance de la classe {0}. Erreur : {1}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoValidConstructor">
<source>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</source>
<target state="new">Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</target>
<note />
</trans-unit>
<trans-unit id="UTA_TestContextSetError">
<source>Unable to set TestContext property for the class {0}. Error: {1}.</source>
<target state="translated">Impossible de définir la propriété TestContext pour la classe {0}. Erreur : {1}.</target>
Expand Down Expand Up @@ -263,11 +268,6 @@ Erreur : {1}</target>
<target state="translated">La méthode {0}.{1} n'existe pas.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoDefaultConstructor">
<source>Unable to get default constructor for class {0}.</source>
<target state="translated">Impossible d'obtenir le constructeur par défaut de la classe {0}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_TestContextLoadError">
<source>Unable to find property {0}.TestContext. Error:{1}.</source>
<target state="translated">Propriété {0}.TestContext introuvable. Erreur :{1}.</target>
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Resources/xlf/Resource.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ ma ha ricevuto {4} argomenti, con tipi "{5}".</target>
<target state="translated">Non è possibile creare un'istanza della classe {0}. Errore: {1}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoValidConstructor">
<source>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</source>
<target state="new">Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</target>
<note />
</trans-unit>
<trans-unit id="UTA_TestContextSetError">
<source>Unable to set TestContext property for the class {0}. Error: {1}.</source>
<target state="translated">Non è possibile impostare la proprietà TestContext per la classe {0}. Errore: {1}.</target>
Expand Down Expand Up @@ -263,11 +268,6 @@ Errore: {1}</target>
<target state="translated">Il metodo {0}.{1} non esiste.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoDefaultConstructor">
<source>Unable to get default constructor for class {0}.</source>
<target state="translated">Non è possibile ottenere il costruttore predefinito per la classe {0}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_TestContextLoadError">
<source>Unable to find property {0}.TestContext. Error:{1}.</source>
<target state="translated">La proprietà {0}.TestContext non è stata trovata. Errore: {1}.</target>
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Resources/xlf/Resource.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ but received {4} argument(s), with types '{5}'.</source>
<target state="translated">クラス {0} のインスタンスを作成できません。エラー: {1}。</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoValidConstructor">
<source>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</source>
<target state="new">Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</target>
<note />
</trans-unit>
<trans-unit id="UTA_TestContextSetError">
<source>Unable to set TestContext property for the class {0}. Error: {1}.</source>
<target state="translated">クラス {0} の TestContext プロパティを設定できません。エラー: {1}。</target>
Expand Down Expand Up @@ -264,11 +269,6 @@ Error: {1}</source>
<target state="translated">メソッド {0}.{1} は存在しません。</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoDefaultConstructor">
<source>Unable to get default constructor for class {0}.</source>
<target state="translated">クラス {0} の既定コンストラクターを取得できません。</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_TestContextLoadError">
<source>Unable to find property {0}.TestContext. Error:{1}.</source>
<target state="translated">プロパティ {0}.TestContext が見つかりません。エラー: {1}。</target>
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Resources/xlf/Resource.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ but received {4} argument(s), with types '{5}'.</source>
<target state="translated">{0} 클래스의 인스턴스를 만들 수 없습니다. 오류: {1}</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoValidConstructor">
<source>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</source>
<target state="new">Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</target>
<note />
</trans-unit>
<trans-unit id="UTA_TestContextSetError">
<source>Unable to set TestContext property for the class {0}. Error: {1}.</source>
<target state="translated">{0} 클래스에 대해 TestContext 속성을 설정할 수 없습니다. 오류: {1}</target>
Expand Down Expand Up @@ -263,11 +268,6 @@ Error: {1}</source>
<target state="translated">{0}.{1} 메서드가 없습니다.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoDefaultConstructor">
<source>Unable to get default constructor for class {0}.</source>
<target state="translated">{0} 클래스의 기본 생성자를 가져올 수 없습니다.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_TestContextLoadError">
<source>Unable to find property {0}.TestContext. Error:{1}.</source>
<target state="translated">{0}.TestContext 속성을 찾을 수 없습니다. 오류: {1}</target>
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Resources/xlf/Resource.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ ale liczba odebranych argumentów to {4} z typami „{5}”.</target>
<target state="translated">Nie można utworzyć wystąpienia klasy {0}. Błąd: {1}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoValidConstructor">
<source>Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</source>
<target state="new">Cannot find a valid constructor for test class '{0}'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.</target>
<note />
</trans-unit>
<trans-unit id="UTA_TestContextSetError">
<source>Unable to set TestContext property for the class {0}. Error: {1}.</source>
<target state="translated">Nie można ustawić właściwości TestContext w klasie {0}. Błąd: {1}.</target>
Expand Down Expand Up @@ -263,11 +268,6 @@ Błąd: {1}</target>
<target state="translated">Metoda {0}.{1} nie istnieje.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_NoDefaultConstructor">
<source>Unable to get default constructor for class {0}.</source>
<target state="translated">Nie można pobrać domyślnego konstruktora dla klasy {0}.</target>
<note></note>
</trans-unit>
<trans-unit id="UTA_TestContextLoadError">
<source>Unable to find property {0}.TestContext. Error:{1}.</source>
<target state="translated">Nie można znaleźć właściwości {0}.TestContext. Błąd:{1}.</target>
Expand Down
Loading