Should I change the naming convention for my unit tests?

后端 未结 7 2497
死守一世寂寞
死守一世寂寞 2021-02-20 04:45

I currently use a simple convention for my unit tests. If I have a class named \"EmployeeReader\", I create a test class named \"EmployeeReader.Tests. I then create all the test

7条回答
  •  时光取名叫无心
    2021-02-20 05:31

    I use second method, and it really helps with describing what your software should do. I also use nested classes to describe more detailed context.

    In essence, test classes are contexts, which can be nested, and methods are all one line assertions. For example,

    public class MyClassSpecification
    {
        protected MyClass instance = new MyClass();
    
        public class When_foobar_is_42 : MyClassSpecification 
        {
            public When_foobar_is_42() {
                this.instance.SetFoobar( 42 ); 
            }
    
            public class GetAnswer : When_foobar_is_42
            {
                private Int32 result;
    
                public GetAnswer() {
                    this.result = this.GetAnswer();
                }
    
                public void should_return_42() {
                    Assert.AreEqual( 42, result );
                }
            }
        }
    }
    

    which will give me following output in my test runner:

    MyClassSpecification+When_foobar_is_42+GetAnswer
        should_return_42
    

提交回复
热议问题