我可以让pytest doctest模块忽略一个文件吗?

时间:2016-12-28 09:11:35

标签: python pytest doctest

我们使用pytest来测试我们的项目,默认情况下启用private void registerUser() { //getting email and password from edit texts final String email = editTextEmail.getText().toString().trim(); String password = editTextPassword.getText().toString().trim(); //checking if email and passwords are empty if (TextUtils.isEmpty(email)) { Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show(); return; } if (TextUtils.isEmpty(password)) { Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show(); return; } //if the email and password are not empty //displaying a progress dialog registerProgressDIg.setMessage("Registering Please Wait..."); registerProgressDIg.show(); // Register the user FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Logger.getLogger(LoginActivity.class.getName()).log(Level.ALL, "createUserWithEmailAndPassword:onComplete:" + task.isSuccessful()); if (task.isSuccessful()) { final ArrayList<String> defaultRoom = new ArrayList<String>(); defaultRoom.add("home"); // Update the user profile information final FirebaseUser user = task.getResult().getUser(); UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder() .setDisplayName(String.valueOf(displayUsername)) .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg")) .build(); user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Logger.getLogger(MainActivity.class.getName()).log(Level.ALL, "User profile updated."); // Construct the ChatUser UserList.user = new ChatUser(user.getUid(),displayUsername, editTextEmail,true,defaultRoom); // Setup link to users database FirebaseDatabase.getInstance().getReference("users").child(user.getUid()).setValue(UserList.user); startActivity(new Intent(MainActivity.this, UserList.class)); finish(); } } }); } else { Logger.getLogger(MainActivity.class.getName()).log(Level.ALL, "createUserWithEmailAndPassword", task.getException()); Utils.showDialog( MainActivity.this, getString(R.string.err_singup)); } } }); } 来收集整个项目的所有doctests。

然而,在测试集合期间可能无法导入一个--doctest-modules,但我无法让pytest忽略它。

我尝试将其放在wsgi.py的{​​{1}}列表中,但显然doctest模块不使用此列表。

唯一可行的是将collect_ignore的整个目录放入conftest.py的pytest配置文件中,但这显然会隐藏整个目录,这是我不想要的。

有没有办法让doctest模块只忽略某个文件?

2 个答案:

答案 0 :(得分:4)

您可以使用hook有条件地从测试发现中排除某些文件夹。 https://docs.pytest.org/en/latest/writing_plugins.html

def pytest_ignore_collect(path, config):
    """ return True to prevent considering this path for collection.
    This hook is consulted for all files and directories prior to calling
    more specific hooks.
    """

答案 1 :(得分:0)

正如MasterAndrey所述,pytest_ignore_collect应该可以解决问题。重要的是要注意,应将conftest.py放到根文件夹(运行测试的文件夹)中。
示例:

import sys

def pytest_ignore_collect(path):
    if sys.version_info[0] > 2:
        if str(path).endswith("__py2.py"):
            return True
    else:
        if str(path).endswith("__py3.py"):
            return True

自pytest v4.3.0起,还有--ignore-glob标志允许按模式忽略。例: pytest --doctest-modules --ignore-glob="*__py3.py" dir/