Unit testing a large project with few publicly accessible components [duplicate]

I’m asking about C#, but this probably applies to most other languages as well.

Imagine I have a project with a lot of complex logic, split up into a lot of small components. Let’s say that, among other things, it contains a ComplexProjectFacade with a single method accepting some parameters and returning some value – and this is the only component that is supposed to be public.

But the logic performed within what is called from that single method is very complex and testing directly on the ComplexProjectFacade gives not nearly enough certainty that all works correctly. What should I do in this case?

  • Put the unit tests in the same project?
  • Make other components publicly accessible only for the sake of testing?
  • Or maybe it is already wrong to make the components internal? Maybe they should be accessible from outside?

6

Extract your encapsulated logic into smaller classes. Keep doing this until you get classes that adhere to the SRP principle and have one, and only one responsibility. Write tests on those classes to test that they perform the little responsibility that they have correctly. Compose your bigger classes from these small classes, until you have the one ComplexProjectFacade which is composed from smaller classes, and so on.

By doing this, you create small, focused and easy-to-understand classes. The fact that they have methods that are public and can therefore be tested is a happy side effect from the fact that you now have code that is easy to understand. Whenever I hear somebody argue about having to make classes or methods public purely for the sake of testing, I try to make them realize that they are also more clearly adhering to principles that lead to better code (e.g. SOLID).

How you do beyond this is more up to personal preference:

  • Putting the tests next to the code in the same assembly pollutes your released code with the tests. They will be in the same DLL which increases size, and so on. You probably do not want to release your compiled test code with your application but if you have no qualms with that, it’s a valid approach. Still requires you to have every class marked as Internal
  • Keeping the classes public means that the exposed interface of your DLL becomes more complex. Users wanting to use your DLL will see a wide range of classes and it might not be immediately obvious what class to use to perform something. Proper usage of namespaces might prevent this somewhat as you can limit the root namespace to the facade and putting all the other classes into separate (sub-)namespaces, making it more obvious what your users need to look at.
  • InternalsVisibleTo allows you to keep all your small classes internal to the assembly, limiting the exposed interface of your DLL to things you really intended to have exposed, but requires you to keep all the classes internal and limits you to having to recompile your library with a new InternalsVisibleTo-attribute if you want to add a new test assembly.

4

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