Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie

I have developed a custom module by inherited the product template. There I have added some custom required field. It works perfectly in local.
But when I pushed the code it fails on test case and build also failed. 
Because of the required field doesn't set while testing.

Awatar
Odrzuć
Najlepsza odpowiedź

Hi,


This is a common issue when you add a required field to a core model like 'product.template'  everything works locally because you're manually filling in the required field, but in automated tests (like Odoo.sh builds or CI pipelines), test records and demo data that create products don't set your new field, causing the tests to fail.

Solution

* Provide a Default Value (Recommended)

Add a default value or compute method so that the field doesn't break existing logic:

from odoo import fields, models

class ProductTemplate(models.Model):
_inherit = 'product.template'

my_required_field = fields.Char(
string="My Required Field",
required=True,
default='Default Value'
)


Hope it helps

Awatar
Odrzuć
Autor

If I set a default value for the field, there's a possibility that someone could inadvertently create a product without altering that default value, which would allow them to submit the product entity. How can this issue be addressed?

Najlepsza odpowiedź

Hello Anup,

Okay, I understand. You've added required fields to the product.template model in a custom module, and your Odoo Cloud tests are failing because these fields aren't being populated during the automated testing process. Here's a breakdown of how to address this, focusing on making your module cloud-test-friendly:

Understanding the Problem

Odoo Cloud tests run automatically when you push code to platforms like Odoo.sh. These tests are designed to ensure your module doesn't break existing functionality. Because your new fields are required, the tests are likely failing when Odoo tries to create or modify product templates without those fields being filled in.

Solutions

Here's a multi-pronged approach to solve this:

  1. Provide Default Values in Tests:
    • Data Files (XML): The best practice is to create XML data files within your module's data/ directory (e.g., data/test_data.xml). These files will be loaded during testing and can be used to create or update product templates, ensuring your required fields are populated.
    <!-- data/test_data.xml -->
    <odoo>
        <data noupdate="1">
            <record id="product_template_test_1" model="product.template">
                <field name="name">Test Product</field>
                <!-- Fill in your required fields here -->
                <field name="your_required_field_1">Some Value</field>
                <field name="your_required_field_2">Another Value</field>
                <field name="type">product</field>
            </record>
        </data>
    </odoo>
    
    • noupdate="1": This is important! It ensures that the data is loaded only during module installation/testing and won't be overwritten during upgrades.
    • Manifest File: Make sure your __manifest__.py file includes the data file:
    # __manifest__.py
    {
        'name': 'Your Module Name',
        'version': '1.0',
        'depends': ['product'],
        'data': [
            'data/test_data.xml',
        ],
    }
    
  2. Test Case Modifications (If Necessary):
    • If your module includes Python-based test cases (in the tests/ directory), you might need to modify those tests to create or update product templates, again ensuring your required fields are set.
    # tests/test_your_module.py
    from odoo.tests import common
    
    class TestYourModule(common.TransactionCase):
    
        def test_product_creation(self):
            product = self.env['product.template'].create({
                'name': 'Test Product',
                'your_required_field_1': 'Value for Test',
                'your_required_field_2': 'Another Test Value',
                'type': 'product',
            })
            self.assertEqual(product.name, 'Test Product')
            # Add more assertions to validate your logic
    
  3. Conditional Requirements (Less Ideal, but Sometimes Necessary):
    • As a last resort (and generally not recommended if you can avoid it), you could make the fields conditionally required. For example, only required if a specific setting is enabled. This adds complexity and might not be the best design.
    from odoo import models, fields, api
    
    class ProductTemplate(models.Model):
        _inherit = 'product.template'
    
        your_required_field_1 = fields.Char(string="Required Field 1")
        your_required_field_2 = fields.Char(string="Required Field 2")
    
        @api.constrains('your_required_field_1', 'your_required_field_2')
        def _check_required_fields(self):
            # Example: Only require the fields if a specific company setting is enabled
            if self.env.company.some_setting:
                for record in self:
                    if not record.your_required_field_1 or not record.your_required_field_2:
                        raise ValidationError("Required fields must be filled in when the setting is enabled.")
    
    • Important: If you use conditional requirements, make sure your tests cover both scenarios (setting enabled and disabled).

Example Scenario

Let's say you added a required field called manufacturer (a Char field) to product.template.

  1. data/test_data.xml:
    <odoo>
        <data noupdate="1">
            <record id="product_template_test_1" model="product.template">
                <field name="name">Test Product</field>
                <field name="manufacturer">Acme Corp</field>
                <field name="type">product</field>
            </record>
        </data>
    </odoo>
    
  2. __manifest__.py:
    {
        'name': 'Product Manufacturer',
        'version': '1.0',
        'depends': ['product'],
        'data': [
            'data/test_data.xml',
        ],
    }
    

Key Considerations

  • Data Consistency: Ensure the data you provide in your test files is valid and consistent with your module's logic.
  • Test Coverage: Write comprehensive tests to cover all possible scenarios and edge cases related to your new fields.
  • Odoo.sh Logs: Carefully examine the Odoo.sh build logs to understand the exact error messages and traceback. This will help you pinpoint the cause of the test failures.
  • Dependencies: Double-check that your module correctly declares its dependency on the product module.
  • Upgradeability: Think about how your module will behave during upgrades. The noupdate="1" attribute in your data files is crucial for this.

By providing default values for your required fields in your test data, you should be able to resolve the test failures and get your module building successfully on the Odoo Cloud platform. Remember to test thoroughly!

🚀 Did This Solve Your Problem?

If this answer helped you save time, money, or frustration, consider:

✅ Upvoting (👍) to help others find it faster

✅ Marking as "Best Answer" if it resolved your issue

Your feedback keeps the Odoo community strong! 💪

(Need further customization? Drop a comment—I’m happy to refine the solution!)

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
cze 25
203
1
cze 25
241
2
mar 25
1067
0
sty 25
1087
0
cze 25
221