跳到正文
Skillpedia
测试48k · #5334

odoo-automated-tests

它能做什么

帮助你为 Odoo 系统编写并运行自动化测试。这能确保新功能不会破坏已有功能。

适用场景

  • ✓创建了新功能,想确认一切仍然正常运行时。
  • ✓想检查系统的某个特定部分是否正确响应时。
  • ✓自动化测试失败,需要弄清原因时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/odoo-automated-tests/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/odoo-automated-tests/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“为医院模块中的新建患者功能编写一个测试,确保姓名不能为空。”

每周 5 个新技能,1 分钟读完。

工作原理

你描述想要测试的内容,工具就会为你生成测试代码。然后,它会告诉你如何在系统中运行该测试。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Odoo Automated Tests

Overview

Odoo has a built-in testing framework based on Python's unittest. This skill helps you write TransactionCase unit tests, HttpCase integration tests, and JavaScript tour tests. It also covers running tests in CI pipelines.

When to Use This Skill

  • Writing unit tests for a custom model's business logic.
  • Creating an HTTP test to verify a controller endpoint.
  • Debugging test failures in a CI pipeline.
  • Setting up automated test execution with --test-enable.

How It Works

  1. Activate: Mention @odoo-automated-tests and describe the feature to test.
  2. Generate: Get complete test class code with setup, teardown, and assertions.
  3. Run: Get the exact odoo CLI command to execute your tests.

Examples

Example 1: TransactionCase Unit Test (Odoo 15+ pattern)

# tests/test_hospital_patient.py
from odoo.tests.common import TransactionCase
from odoo.tests import tagged
from odoo.exceptions import ValidationError

@tagged('post_install', '-at_install')
class TestHospitalPatient(TransactionCase):

    @classmethod
    def setUpClass(cls):
        # Use setUpClass for performance — runs once per class, not per test
        super().setUpClass()
        cls.Patient = cls.env['hospital.patient']
        cls.doctor = cls.env['res.users'].browse(cls.env.uid)

    def test_create_patient(self):
        patient = self.Patient.create({
            'name': 'John Doe',
            'doctor_id': self.doctor.id,
        })
        self.assertEqual(patient.state, 'draft')
        self.assertEqual(patient.name, 'John Doe')

    def test_confirm_patient(self):
        patient = self.Patient.create({'name': 'Jane Smith'})
        patient.action_confirm()
        self.assertEqual(patient.state, 'confirmed')

    def test_empty_name_raises_error(self):
        with self.assertRaises(ValidationError):
            self.Patient.create({'name': ''})

    def test_access_denied_for_other_user(self):
        # Test security rules by running as a different user
        other_user = self.env.ref('base.user_demo')
        with self.assertRaises(Exception):
            self.Patient.with_user(other_user).create({'name': 'Test'})

setUpClass vs setUp: Use setUpClass (Odoo 15+) for shared test data. It runs once per class and is significantly faster than setUp which re-initializes for every single test method.

Example 2: Run Tests via CLI

# Run all tests for a specific module
./odoo-bin --test-enable --stop-after-init -d my_database -u hospital_management

# Run only tests tagged with a specific tag
./odoo-bin --test-enable --stop-after-init -d my_database \
  --test-tags hospital_management

# Run a specific test class
./odoo-bin --test-enable --stop-after-init -d my_database \
  --test-tags /hospital_management:TestHospitalPatient

Example 3: HttpCase for Controller Testing

from odoo.tests.common import HttpCase
from odoo.tests import tagged

@tagged('post_install', '-at_install')
class TestPatientController(HttpCase):

    def test_patient_page_authenticated(self):
        # Authenticate as a user, not with hardcoded password
        self.authenticate(self.env.user.login, self.env.user.login)
        resp = self.url_open('/hospital/patients')
        self.assertEqual(resp.status_code, 200)

    def test_patient_page_redirects_unauthenticated(self):
        # No authenticate() call = public/anonymous user
        resp = self.url_open('/hospital/patients', allow_redirects=False)
        self.assertIn(resp.status_code, [301, 302, 403])

Best Practices

  • ✅ Do: Use setUpClass() with cls.env instead of setUp() — it is dramatically faster for large test suites.
  • ✅ Do: Use @tagged('post_install', '-at_install') to run tests after all modules are installed.
  • ✅ Do: Test both the happy path and error conditions (ValidationError, AccessError, UserError).
  • ✅ Do: Use self.with_user(user) to test access control without calling sudo().
  • ❌ Don't: Use a production database for tests — always use a dedicated test database.
  • ❌ Don't: Rely on test execution order — each TransactionCase test is rolled back in isolation.
  • ❌ Don't: Hardcode passwords in HttpCase.authenticate() — use self.env.user.login or a fixture user.

Limitations

  • JavaScript tour tests require a running browser (via phantomjs or Chrome headless) and a live Odoo server — not covered in depth here.

…

常见问题

TransactionCase 和 HttpCase 有什么区别?

TransactionCase 测试系统的内部逻辑(如计算或业务规则),而 HttpCase 测试系统如何响应网络请求(如访问某个页面)。

这个工具适用于所有 Odoo 版本吗?

该工具基于 Odoo 的测试框架,而这个框架存在于多个版本中。示例展示的是较新的写法,但其思路同样适用。

什么是“CI 测试”?

CI 是持续集成(Continuous Integration)的缩写。这类测试会在代码每次更新时自动运行,以便快速发现错误。

详情

作者的原始描述

Write and run Odoo automated tests using TransactionCase, HttpCase, and browser tour tests. Covers test data setup, mocking, and CI integration.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#5334
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能