2017-08-23 2 views

Répondre

0

J'ai une classe BaseTest héritée par toutes les classes de test avec un attribut PlatformTestFixture. Ensuite, nous pouvons commencer à Android ou iOS app basée sur le paramètre de plate-forme qui est passé dans.

public class PlatformTestFixtureAttribute : TestFixtureAttribute 
{ 
    public PlatformTestFixtureAttribute() 
    { 
    } 

    public PlatformTestFixtureAttribute(params object[] arguments) 
     : base(arguments) 
    { 
     AddPlatformCategory(arguments); 
    } 

    private void AddPlatformCategory(object[] args) 
    { 
     // Not needed in TestCloud 
     if (!TestEnvironment.IsTestCloud) 
     { 
      foreach (var arg in args) 
      { 
       if (arg is Platform) 
       { 
        if (Platform.Android == (Platform)arg) 
        { 
         AddAndroidCategory(); 
        } 
        else if (Platform.iOS == (Platform)arg) 
        { 
         AddiOSCategory(); 
        } 
       } 
      } 
     } 
    } 

    private void AddAndroidCategory() 
    { 
     Category = "AndroidTest"; 
    } 

    private void AddiOSCategory() 
    { 
     Category = "iOSTest"; 
    } 
} 

[PlatformTestFixture(Platform.Android)] 
[PlatformTestFixture(Platform.iOS)] 
public abstract class BaseTest 
{ 
    Platform _platform; 
    protected BaseTest(Platform platform) 
    { 
     _platform = platform; 
    } 

    [SetUp] 
    public virtual void BeforeEachTest() 
    { 
     if (platform == Platform.Android) 
     { 
      StartAndroidApp(); 
     } 
     else 
     { 
      StartiOSApp(); 
     } 
    } 
} 

public class ChildTest : BaseTest 
{ 
    public ChildTest(Platform platform) 
     : base(platform) 
    { 
    } 

    [Test] 
    public void SomeTest() 
    { 
     ... 
    } 
} 

Ensuite, je peux exécuter le test en ligne de commande indiquant si vous souhaitez exécuter iOSTest ou AndroidTest.

[NUnit] [Test Fixture DLL Path] -include=AndroidTest 
OR 
[NUnit] [Test Fixture DLL Path] -include=iOSTest