在单元测试中我想让 一个方法抛异常的话就测试成功,不异常的话就失败.应该怎么写?

解决方案 »

  1.   

    try
    {
        YourMethod();
        return false;
    }
    catch
    {
        return true;
    }
      

  2.   

    try
    {
        Assert.Fail();
    }
    catch
    {}
      

  3.   

    我是想在单元测试中写这个啊,带[TestMethod()]的方法里
    即使try{  
        MyMethod();
        Assert.Fail();
    }catch{}
    如果MyMethod()抛出异常的话 单元测试也一样会失败
      

  4.   

            /// <summary>
            ///A test for a
            ///</summary>
            [TestMethod()]
            [DeploymentItem("ConsoleApplication1.exe")]
            public void aTest()
            {
                Program_Accessor target = new Program_Accessor(); // TODO: Initialize to an appropriate value
                bool bThrow = false;
                try
                {
                    target.a();
                }
                catch
                {
                    bThrow = true;
                }
                if (!bThrow) Assert.Fail();        }