Unit testing of non-atomic methods [duplicate]

I’m new in unit testing. For last two days I’m thinking how to test non-atomic methods with unit tests.
When i need to write tests for some simple, atomic method situation in pretty easy – i’m taking all scenarios for this method that i can imagine, and reforge each of it in unit test.

But what if i have methods that are calling other methods? Should I test each scenarios for tested method AND each methods it is calling?

Maybe some example will be easier to understand:

    public int makeSomeOperation(int a, int b)
    {
        if (a < 0)
            throw new ArgumentException("a must be greater that 0");
        if (b == 0)
            throw new ArgumentException("b cannot be 0");
        return a/b;
    }

    public bool SomeNonatomicMethod(int number)
    {
        if (number * number % 2 == 1)
            return false;
        int secondNumber = getNumberFromMordor();
        int result = makeSomeOperation(number, secondNumber);
        if (result == "123")
            return true;
        return false;
    }

In this case, makeSomeOperation is easy to test – one test for each exception, one to check in returning result is equals with expectations.

But what about SomeNonatomicMethod? Did I need to write not only test for each case in this method (so: power of number is odd, result is equal 123, and result is diffrent), but also each possible flow of things that may happen in getNumberFromMordor and makeSomeOperation?

Things that may happen in makeSomeOperation are already tested, so are they should be tested second time?

1

If getNumberFromMordor and makeSomeOperation are public, they should be tested by themselves, so your unit test should not doublecheck that getNumberFromMordor works correctly.

Only test what SomeNonatomicMethod does. Don’t re-test public methods it relies on.

If getNumberFromMordor is private, you should test SomeNonatomicMethod against the possibility that getNumberFromMordor fails to work properly.

If SomeNonatomicMethod itself does so many things that you end up with dozens of assertions, it is a signal that it doesn’t follow the single responsibility principle and should be broken down.

Usually you would not test the “makeSomeOperation” method. However, this is not a universal rule. Why? Because your composite method may not supply an adequate combination of parameters to execute all of “makeSomeOperation”‘s code.

So, what you’re really looking for is code coverage. When you write unit tests, and you’ve got a good code coverage tool, once you get to somewhere between 80% and 90% code coverage for a large library, you’re doing pretty good. Ideally you’d get 100%, but that’s almost impossible to maintain on a code base of any size. Maybe if the code you were writing was running in a medical device or something you’d try to maintain 100%, but for we mere mortals, we try to target and maintain 80% to 90% code coverage.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Unit testing of non-atomic methods [duplicate]

I’m new in unit testing. For last two days I’m thinking how to test non-atomic methods with unit tests.
When i need to write tests for some simple, atomic method situation in pretty easy – i’m taking all scenarios for this method that i can imagine, and reforge each of it in unit test.

But what if i have methods that are calling other methods? Should I test each scenarios for tested method AND each methods it is calling?

Maybe some example will be easier to understand:

    public int makeSomeOperation(int a, int b)
    {
        if (a < 0)
            throw new ArgumentException("a must be greater that 0");
        if (b == 0)
            throw new ArgumentException("b cannot be 0");
        return a/b;
    }

    public bool SomeNonatomicMethod(int number)
    {
        if (number * number % 2 == 1)
            return false;
        int secondNumber = getNumberFromMordor();
        int result = makeSomeOperation(number, secondNumber);
        if (result == "123")
            return true;
        return false;
    }

In this case, makeSomeOperation is easy to test – one test for each exception, one to check in returning result is equals with expectations.

But what about SomeNonatomicMethod? Did I need to write not only test for each case in this method (so: power of number is odd, result is equal 123, and result is diffrent), but also each possible flow of things that may happen in getNumberFromMordor and makeSomeOperation?

Things that may happen in makeSomeOperation are already tested, so are they should be tested second time?

1

If getNumberFromMordor and makeSomeOperation are public, they should be tested by themselves, so your unit test should not doublecheck that getNumberFromMordor works correctly.

Only test what SomeNonatomicMethod does. Don’t re-test public methods it relies on.

If getNumberFromMordor is private, you should test SomeNonatomicMethod against the possibility that getNumberFromMordor fails to work properly.

If SomeNonatomicMethod itself does so many things that you end up with dozens of assertions, it is a signal that it doesn’t follow the single responsibility principle and should be broken down.

Usually you would not test the “makeSomeOperation” method. However, this is not a universal rule. Why? Because your composite method may not supply an adequate combination of parameters to execute all of “makeSomeOperation”‘s code.

So, what you’re really looking for is code coverage. When you write unit tests, and you’ve got a good code coverage tool, once you get to somewhere between 80% and 90% code coverage for a large library, you’re doing pretty good. Ideally you’d get 100%, but that’s almost impossible to maintain on a code base of any size. Maybe if the code you were writing was running in a medical device or something you’d try to maintain 100%, but for we mere mortals, we try to target and maintain 80% to 90% code coverage.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Unit testing of non-atomic methods [duplicate]

I’m new in unit testing. For last two days I’m thinking how to test non-atomic methods with unit tests.
When i need to write tests for some simple, atomic method situation in pretty easy – i’m taking all scenarios for this method that i can imagine, and reforge each of it in unit test.

But what if i have methods that are calling other methods? Should I test each scenarios for tested method AND each methods it is calling?

Maybe some example will be easier to understand:

    public int makeSomeOperation(int a, int b)
    {
        if (a < 0)
            throw new ArgumentException("a must be greater that 0");
        if (b == 0)
            throw new ArgumentException("b cannot be 0");
        return a/b;
    }

    public bool SomeNonatomicMethod(int number)
    {
        if (number * number % 2 == 1)
            return false;
        int secondNumber = getNumberFromMordor();
        int result = makeSomeOperation(number, secondNumber);
        if (result == "123")
            return true;
        return false;
    }

In this case, makeSomeOperation is easy to test – one test for each exception, one to check in returning result is equals with expectations.

But what about SomeNonatomicMethod? Did I need to write not only test for each case in this method (so: power of number is odd, result is equal 123, and result is diffrent), but also each possible flow of things that may happen in getNumberFromMordor and makeSomeOperation?

Things that may happen in makeSomeOperation are already tested, so are they should be tested second time?

1

If getNumberFromMordor and makeSomeOperation are public, they should be tested by themselves, so your unit test should not doublecheck that getNumberFromMordor works correctly.

Only test what SomeNonatomicMethod does. Don’t re-test public methods it relies on.

If getNumberFromMordor is private, you should test SomeNonatomicMethod against the possibility that getNumberFromMordor fails to work properly.

If SomeNonatomicMethod itself does so many things that you end up with dozens of assertions, it is a signal that it doesn’t follow the single responsibility principle and should be broken down.

Usually you would not test the “makeSomeOperation” method. However, this is not a universal rule. Why? Because your composite method may not supply an adequate combination of parameters to execute all of “makeSomeOperation”‘s code.

So, what you’re really looking for is code coverage. When you write unit tests, and you’ve got a good code coverage tool, once you get to somewhere between 80% and 90% code coverage for a large library, you’re doing pretty good. Ideally you’d get 100%, but that’s almost impossible to maintain on a code base of any size. Maybe if the code you were writing was running in a medical device or something you’d try to maintain 100%, but for we mere mortals, we try to target and maintain 80% to 90% code coverage.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật