Are branches in unit tests bad practice?

Say I want to assert that a certain option menu is displayed in the front-end of a project. The menu is on screen X and I have to first select data in Y to get to X. Now, it doesn’t matter at all what data I select in Y, it will always lead to X.

But sometimes, depending on the data, I will get a screen Z which I can just skip through. Now obviously, if I write the test in a deterministic manner the test may fail if the data I selected leads to screen Z.

The data I’m talking about is test data, set up before every test to the same state. So theoretically, I could just select one data entry that I know will not go to screen Z.

But, I could also simply include a check to see if I’m on screen Z after selecting the data and skip Z. Now, even changes to the test data (though rare, it happens) would not break my test.

Somehow I don’t like this approach but I can’t really explain why. Adding a method to check for Z would add a (probably) “useless” method to my test framework.

So my question is, are branches to handle conditional behaviour that is not being tested in that specific test okay? Are there any problems with this approach? Is it good to do this?

5

It’s not unreasonable for a change in test data to change the outcome of a test.

I wouldn’t put the conditional branch in, I would ensure that I have a separate test with a set of test data that always goes to screen Z so that behaviour can be tested.

If the initial test you’re trying to do is that when data from Y is entered, screen X is displayed then that’s exactly what you should test – don’t overcomplicate tests.

Y -> Z -> X is not the same as Y -> X and I presume some business logic (which should be tested) controls the appearance of Z?

2

I would avoid branches in the testing code- I would be wanting to find the exact conditions that took me along the X->Y path and test the commonest and boundary paths for that. Then I would identify the conditions that took the user along the X -> Z -> Y path and write a separate test for the boundary conditions of that.

If you aren’t using completely deterministic data/mocks so that your tests don’t behave totally predictably, you aren’t writing unit tests.

If the two tests have a lot in common, you might want to put the shared code into helper or setup/takedown methods so you aren’t repeating yourself too much inside your tests.

Depending on the platform you are working with it can be very helpful to have parameterised tests so you can run different data through the same test to cover different boundary conditions.

In general when you are testing, you want to avoid any uncertainty and you want to keep the test to the point. Uncertainty/randomness in the data or in the test logic will only make things more complicated/less understandable to you or other developers which leads to a maintenance nightmare. This is true for all tests across the board.

This means only set up data (if possible) that is required to test that one thing. This will make the tests easier to understand and maintain because there is less distractions from what matters. It also makes tests more decoupled from each other because the data you are using for the one functionality does not interfere with other tests. For smaller applications with a small test suite you might get away with ‘single large test data’ up front. However, maintaining such large data set becomes a nightmare when you are working with a larger application.

As far as the test logic is concerned, you do not want to clutter it with conditionals like if by any chance I got this intermediate thing, ignore it and continue. In a test you should always know what action leads to what state so you should specifically code for it. Such conditional statements only distract from what is important about the test and may lead to more code that simply deals with these ‘possibilities’ than the test code itself. Another problem with this approach is that the application and test code can get out of sync. As the application evolves you remove state Z but it does not get cleaned up from the test code itself because it was there only conditionally (tests will not break, therefore not signal that there is a problem). This can be extremely stressful especially for someone who is new to the code base because now they have to dig to see what this code is all about.

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

Are branches in unit tests bad practice?

Say I want to assert that a certain option menu is displayed in the front-end of a project. The menu is on screen X and I have to first select data in Y to get to X. Now, it doesn’t matter at all what data I select in Y, it will always lead to X.

But sometimes, depending on the data, I will get a screen Z which I can just skip through. Now obviously, if I write the test in a deterministic manner the test may fail if the data I selected leads to screen Z.

The data I’m talking about is test data, set up before every test to the same state. So theoretically, I could just select one data entry that I know will not go to screen Z.

But, I could also simply include a check to see if I’m on screen Z after selecting the data and skip Z. Now, even changes to the test data (though rare, it happens) would not break my test.

Somehow I don’t like this approach but I can’t really explain why. Adding a method to check for Z would add a (probably) “useless” method to my test framework.

So my question is, are branches to handle conditional behaviour that is not being tested in that specific test okay? Are there any problems with this approach? Is it good to do this?

5

It’s not unreasonable for a change in test data to change the outcome of a test.

I wouldn’t put the conditional branch in, I would ensure that I have a separate test with a set of test data that always goes to screen Z so that behaviour can be tested.

If the initial test you’re trying to do is that when data from Y is entered, screen X is displayed then that’s exactly what you should test – don’t overcomplicate tests.

Y -> Z -> X is not the same as Y -> X and I presume some business logic (which should be tested) controls the appearance of Z?

2

I would avoid branches in the testing code- I would be wanting to find the exact conditions that took me along the X->Y path and test the commonest and boundary paths for that. Then I would identify the conditions that took the user along the X -> Z -> Y path and write a separate test for the boundary conditions of that.

If you aren’t using completely deterministic data/mocks so that your tests don’t behave totally predictably, you aren’t writing unit tests.

If the two tests have a lot in common, you might want to put the shared code into helper or setup/takedown methods so you aren’t repeating yourself too much inside your tests.

Depending on the platform you are working with it can be very helpful to have parameterised tests so you can run different data through the same test to cover different boundary conditions.

In general when you are testing, you want to avoid any uncertainty and you want to keep the test to the point. Uncertainty/randomness in the data or in the test logic will only make things more complicated/less understandable to you or other developers which leads to a maintenance nightmare. This is true for all tests across the board.

This means only set up data (if possible) that is required to test that one thing. This will make the tests easier to understand and maintain because there is less distractions from what matters. It also makes tests more decoupled from each other because the data you are using for the one functionality does not interfere with other tests. For smaller applications with a small test suite you might get away with ‘single large test data’ up front. However, maintaining such large data set becomes a nightmare when you are working with a larger application.

As far as the test logic is concerned, you do not want to clutter it with conditionals like if by any chance I got this intermediate thing, ignore it and continue. In a test you should always know what action leads to what state so you should specifically code for it. Such conditional statements only distract from what is important about the test and may lead to more code that simply deals with these ‘possibilities’ than the test code itself. Another problem with this approach is that the application and test code can get out of sync. As the application evolves you remove state Z but it does not get cleaned up from the test code itself because it was there only conditionally (tests will not break, therefore not signal that there is a problem). This can be extremely stressful especially for someone who is new to the code base because now they have to dig to see what this code is all about.

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