小编Dev*_*eer的帖子

即使执行了测试,Jacoco 报告聚合也显示 0 覆盖率

我的maven项目目录结构如下:

parent
  -child
    -src/java
    -src/test <!-- unit tests -->
    -pom.xml
  -integration_test
    -src/test
    -pom.xml
  -report
    -pom.xml
  -pom.xml
Run Code Online (Sandbox Code Playgroud)

由于集成测试和源代码位于不同的模块中,因此我使用了 Jacoco report-aggragate (报告模块)。

父pom.xml

        <profile>
            <id>codecoverage-it</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.8.4</version>
                        <executions>
                            <execution>
                                <id>prepare-agent</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>prepare-agent-integration</id>
                                <goals>
                                    <goal>prepare-agent-integration</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
Run Code Online (Sandbox Code Playgroud)

报告模块pom.xml

<dependencies>
        <dependency>
            <groupId>###</groupId>
            <artifactId>child</artifactId>
            <version>${project.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>###</groupId>
            <artifactId>integration_test</artifactId>
            <version>${project.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>overage-aggregate</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>report-aggregate</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>report-aggregate</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin> …
Run Code Online (Sandbox Code Playgroud)

integration-testing jacoco jacoco-maven-plugin

7
推荐指数
0
解决办法
1012
查看次数

非调试模式下的 Django Whitenoise 500 服务器错误

我在本地机器上使用 django。为了提供静态文件,我将 WhiteNoise 与它一起使用。当DEBUG = True所有静态文件都正确提供时。但是当我更改DEBUG = False并设置时,ALLOWED_HOSTS = ['*']我收到了 500 服务器错误。但是管理站点加载没有任何错误。此外,当我注释掉时,STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'我没有收到 500 错误。

我按照http://whitenoise.evans.io/en/stable/django.html 中给出的文档来连接 whitenoise。我没有对wsgi.py文件进行任何更改。我跑了python manage.py collecststatic,它没有任何错误地运行。

下面settings.py给出:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'fdft&b(xb*!qq3ghjkjhg6789ih8ik!w10$0uscxcpqpmz'
DEBUG = False

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
'whitenoise.runserver_nostatic', #Disable Djangos static file server during DEVELOPMENT
'gep_app.apps.GepAppConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', …
Run Code Online (Sandbox Code Playgroud)

django django-settings django-staticfiles collectstatic whitenoise

5
推荐指数
1
解决办法
1873
查看次数

使用 SelectMultiple 渲染的 Django ArrayField 不将所选选项显示为“已选择”

我正在尝试使用这样的 SelectMultiple 小部件渲染 postgres Arrayfield 值

在 models.py 中:

SLOT_CHOICES = (('A','Slot A'),('B','Slot B'),('C','Slot C'),('D','Slot D'),('E','Slot E'),('F','Slot F'),('G','Slot G'),('H','Slot H'),('P','Slot P'),('Q','Slot Q'),('R','Slot R'),('S','Slot S'),('T','Slot T'),)
core_slots = ArrayField(models.CharField(max_length=1,choices=SLOT_CHOICES),blank=True,null=True)
Run Code Online (Sandbox Code Playgroud)

在forms.py中:

    self.fields['core_slots'].widget = forms.SelectMultiple(attrs={
            'placeholder': 'Choose slots',
            'class': 'multi-select-input',
        },choices=self.Meta.model.SLOT_CHOICES)
Run Code Online (Sandbox Code Playgroud)

如果我选择一个选项并提交,然后尝试使用“实例”获取填写的表单,那么它可以正常工作并将先前选择的选项显示为“已选择”。但是,如果我选择多个选项,那么在提交时,所有选定的值都会正确插入到数据库中,但如果我尝试使用“实例”获取填写的表单,那么它不会显示任何选定的选项。

对于 ManyToManyField 不会出现此问题。

在 models.py 中:

past_courses = models.ManyToManyField(Course,blank=True)
Run Code Online (Sandbox Code Playgroud)

在forms.py中:

    self.fields['past_courses'].queryset = Course.objects.filter(~Q(dept=self.instance.dept)).order_by('name')
    self.fields['past_courses'].widget.attrs.update({
            'placeholder': 'Choose past courses',
            'class': 'multi-select-input',
        })
Run Code Online (Sandbox Code Playgroud)

这个效果很好。只有使用 SelectMultiple 小部件渲染的 ArrayField 存在问题。我想在模板中显示提交的选项,有什么方法可以解决此问题,或者是否有其他方法可以在模板中显示选定的选项。

python django django-models django-forms

2
推荐指数
1
解决办法
2323
查看次数