C# 如何使用框架或内置功能以步骤的形式执行代码?

C# 如何使用框架或内置功能以步骤的形式执行代码?,c#,C#,我正在寻找一个框架或一种内置的方式来编写优雅的代码,这些代码易于维护,可以完成以下任务 例如: 在我的主要方法中,我想做3件事: 买一块蛋糕 在蛋糕上涂上糖霜 上蛋糕 仅当所有3个动作完成后,我认为操作是成功的。 如果某个特定操作失败,我想知道失败的是什么 要解决此问题,我的主要方法可能会是这样: public void IsBirthdayAsuccess() { var birthdayManager = new birthdayManager(); try {

我正在寻找一个框架或一种内置的方式来编写优雅的代码,这些代码易于维护,可以完成以下任务

例如:

在我的主要方法中,我想做3件事:

  • 买一块蛋糕
  • 在蛋糕上涂上糖霜
  • 上蛋糕
  • 仅当所有3个动作完成后,我认为操作是成功的。 如果某个特定操作失败,我想知道失败的是什么

    要解决此问题,我的主要方法可能会是这样:

    public void IsBirthdayAsuccess()
    {
       var birthdayManager = new birthdayManager(); 
    
       try
         {
               var cake = birthdayManager.BuyCake(price.cheap, quality.good, delivery.fast);
               Assert.isTrue(cake.ArrivedQuick && cake.IsGood && cake.IsQuick, "Supplier didn't get the cake right"); 
         }
       catch
         {
              Assert.Fail("The birthday operation has failed on step 1 : Buying the cake failed"); 
         }
    
       try
         {
             birthdayManager.Cakes[0].PutIcingOn("Vanilla");
             Assert.IsTrue(birthdayManager.Cakes[0].IsIced(), "Icing the cake didn't work"); 
    
         }
       catch
         {
             Assert.Fail("The birthday operation has failed on step 2 : Putting icing on the cake");
         }
    
       try
         {
             Party.ServeCake(birthdayManager.Cakes[0]);
             Assert.IsTrue(birthdayManager.Cakes[0].IsServed(), "Cake couldn't be served"); 
    
         }
       catch
         {
             Assert.Fail("The birthday operation has failed on step 3 : Serving the cake");
         }
    } 
    
    public void TestMethod(int id)
    {
       var test = new test(id);
    
       TestSteps
        {
          ["Login to application"]
          TestStep
            {
                test.LoginToApplication(); 
            }
          Assert, "You are not logged in"
            {
                test.IsLoggedIn(); 
            }
          Failure
            {
                Assert.Fail("Test failed on step " + testContext.TestStep.Index); 
            }
    
        }  
    
    现在实际上,这里的“步骤”是由代码中的一系列try/catch块表示的,但实际上是在企业系统的其他地方。这就是为什么我在寻找一个框架,它可以在函数中提供一个“步骤”框架的更一致的实现

    我想自己滚,但可能还有别的东西存在

    如果我自己滚,结果会是这样:

    public void IsBirthdayAsuccess()
    {
       var birthdayManager = new birthdayManager(); 
    
       try
         {
               var cake = birthdayManager.BuyCake(price.cheap, quality.good, delivery.fast);
               Assert.isTrue(cake.ArrivedQuick && cake.IsGood && cake.IsQuick, "Supplier didn't get the cake right"); 
         }
       catch
         {
              Assert.Fail("The birthday operation has failed on step 1 : Buying the cake failed"); 
         }
    
       try
         {
             birthdayManager.Cakes[0].PutIcingOn("Vanilla");
             Assert.IsTrue(birthdayManager.Cakes[0].IsIced(), "Icing the cake didn't work"); 
    
         }
       catch
         {
             Assert.Fail("The birthday operation has failed on step 2 : Putting icing on the cake");
         }
    
       try
         {
             Party.ServeCake(birthdayManager.Cakes[0]);
             Assert.IsTrue(birthdayManager.Cakes[0].IsServed(), "Cake couldn't be served"); 
    
         }
       catch
         {
             Assert.Fail("The birthday operation has failed on step 3 : Serving the cake");
         }
    } 
    
    public void TestMethod(int id)
    {
       var test = new test(id);
    
       TestSteps
        {
          ["Login to application"]
          TestStep
            {
                test.LoginToApplication(); 
            }
          Assert, "You are not logged in"
            {
                test.IsLoggedIn(); 
            }
          Failure
            {
                Assert.Fail("Test failed on step " + testContext.TestStep.Index); 
            }
    
        }  
    
    是否存在这样的框架

    编辑:同样为了重用,我可以迭代“步骤”集合和“步骤”项,这意味着我可以使用它在excel或其他地方进行报告

    编辑2:“滚动您自己的”示例旨在替换try/catch和flow控件的语义,C#这是可扩展的吗

    看看SpecFlow:,它是一个非常棒的基于步骤的测试框架。

    这对您的案例不起作用吗?