Is there a similar option to ImportOption.DoNotIncludeTests on a per test level in ArchUnit

huangapple go评论96阅读模式
英文:

Is there a similar option to ImportOption.DoNotIncludeTests on a per test level in ArchUnit

问题

在ArchUnit中,是否可以在每个测试基础上以编程方式从测试包中筛选出类?

这应该基本上与@AnalyzeClasses(packages = "....", importOptions = ImportOption.DoNotIncludeTests.class)在类级别上的行为相同。

背景是我们有一些ArchTests应该针对所有类运行,但也有一些只应该针对不在测试位置的类运行。

也许类似这样的方式?

Architectures.onionArchitecture()
        ....
        .check(classes.that(areNotTests())); ??
英文:

My question is if we can filter out classes from the test packages programmatically on a per test basis in ArchUnit ?

It should be basically the same behaviour that @AnalyzeClasses(packages = "....", importOptions = ImportOption.DoNotIncludeTests.class) does on a class level.

Background is that we have some ArchTests that should run against all classes but also some which only should run against classes that are not in the test location.

Something like that maybe?

    Architectures.onionArchitecture()
            ....
            .check(classes.that(areNotTests())); ??

(We can achieve the goal by creating two Classes that hold the ArchUnit tests seperately and only add the ImportOption.DoNotIncludeTests to the one of them. But we would rather have one set of classes to analyze and decide on a per test level. If there is a way to achieve that)

答案1

得分: 1

ImportOption 操作 Location,但你可以尝试使用以下适配器:

DescribedPredicate<JavaClass> notTests = DescribedPredicate.describe("not tests",
    javaClass -> javaClass.getSource()
            .map(Source::getUri)
            .map(Location::of)
            .map(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS::includes)
            .orElse(true)  // test classes should have a source;
         //  alternatively, you could probably also use:
         // .orElseThrow(IllegalStateException::new)
);
英文:

ImportOptions operate on Locations, but you can try to use the following adapter:

DescribedPredicate<JavaClass> notTests = DescribedPredicate.describe("not tests",
    javaClass -> javaClass.getSource()
            .map(Source::getUri)
            .map(Location::of)
            .map(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS::includes)
            .orElse(true)  // test classes should have a source;
         //  alternatively, you could probably also use:
         // .orElseThrow(IllegalStateException::new)
);

huangapple
  • 本文由 发表于 2023年4月13日 22:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定