2

Consider I have a C# test project. Each test executes some code and that code could exist in other files that could be local to the project or could exist in another project in the same solution, or even in a project in another solution. How might I find out which files are "used" by my test?

It would be preferable that the solution requires no changes to the tests. It would also be preferable that the approach can be automated.

For example, say I have:

Test.cs

[Test]
public void Test() {
    var a = new Foo();
    a.bar();
}

Foo.cs

public class Foo {
    public void bar() { ... }
}

I would like to know that test Test() depends on Foo.cs.

David Poxon
  • 2,435
  • 4
  • 23
  • 44
  • By writing tests that test for a **single** thing, usually a single method within a single class. Anyway the test does not know **anything** about your soruce-code. So unless you don´t give your tests meaningful names, you´re stuck on the tests being self-explanatory. – MakePeaceGreatAgain Jul 03 '19 at 07:03
  • It's not practical to have unit tests that are only dependent on a single method in a single class. – David Poxon Jul 03 '19 at 07:12
  • Maybe let Visual Studio create a code map on your unit tests? – NotFound Jul 03 '19 at 07:13
  • 2
    "It's not practical to have unit tests that are only dependent on a single method in a single class" but that´s what unit-tests are about. Anything else woulb be an integration-test, where you check an entire component together with its dependencies. – MakePeaceGreatAgain Jul 03 '19 at 07:21
  • I have updated my question to avoid confusion. – David Poxon Jul 03 '19 at 07:24
  • @Bleep-Bloop Thanks, that's a nice lead. I will check to see if it can be automated. – David Poxon Jul 03 '19 at 07:27
  • 1
    @DavidPoxon if you want to spend time, I am pretty sure that you can check out the dependcies with roslyn-framework. [Get dependencies between classes in Roslyn](https://stackoverflow.com/questions/39235100/get-dependencies-between-classes-in-roslyn) – Mar Tin Jul 03 '19 at 07:35
  • I repeat myself: the test doesn´t know **anything** about your source-code, it only knows the compiled code. Imagine your class `Foo` was `partial` and consists of two different sourcecode-files, `Foo1.cs` and `Foo2.cs`. For the test those files don´t exist, only the (compiled) class `Foo`. – MakePeaceGreatAgain Jul 03 '19 at 07:40
  • I'm happy to do a static analysis, but was wondering if there might be an existing solution or simpler approach that I'm not aware of. – David Poxon Jul 03 '19 at 07:42
  • @HimBromBeere but why are you making that point? I haven't mentioned anything about compiled code or a run-time analysis. – David Poxon Jul 03 '19 at 08:01

0 Answers0