English 中文(简体)
Unit testing. File structure
原标题:

I have a C++ legacy codebase with 10-15 applications, all sharing several components.

While setting up unittests for both shared components and for applications themselves, I was wondering if there are accepted/common file structures for this.

Because my unit tests have several base classes in order to simplify project/customer specific test setups, there are alot of files that are common for all tests.

To me it seems natural here to create a new directory that contains all the test related files, mocks etc -to have it all centralized, and also keep testing related definitions out of the main make files.

On the other hand I see that it is common practice to have the test files reside together with the code files that they test.

Is there a more/less accepted way of doing this?

最佳回答

Out of sight, out of mind; if you keep the test files together with the code files it may be more obvious to the developers that when they update a code file they should update the tests as well.

问题回答

As you noted, there are two common ways to locate unit test files: near the implementation code they are testing, and in a separate file hierarchy. The choice is a matter of what is the common practice in your organisation and personal taste.

Regarding the location of common test code, just organize your test code are you would organize implementation code.

In your particular case, if some test infrastructure is common to several independent components, it would be a good idea to create a new component (call it "testing", for example) that other components depends on for their tests, instead of adding dependences between existing components.

I usually organize such code in a file structure that looks (in a simple case) like this:

apps
    app1
        app1module1
        app2module2
        app1tests
    app2
        app2module1
        app2tests
components
    comp1
        comp1module1
        comp1module2
        comp1tests
common_test_stuff

There is no single right way to do this, but this seems to be a common practice that keeps production and test code separate and attempts to remove the out-of-sight, out-of-mind problem (mentioned by zac) at the same time.

Keep the test code close to the product code, and arrange your Makefile (or whatever you re using) so that the tests compile at the same time as the test, to make them visible, especially if not everyone in the team is writing tests.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签