Try following way:
protected $eavAttributeFactory;
protected $attributeOptionManagement;
public function __construct(
\Magento\Eav\Model\Entity\AttributeFactory $eavAttributeFactory,
\Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManagement
) {
$this->eavAttributeFactory = $eavAttributeFactory;
$this->attributeOptionManagement = $attributeOptionManagement;
}
And then
$magentoAttribute = $this->eavAttributeFactory->create()->loadByCode('catalog_product', 'test_attribute');
$attributeCode = $magentoAttribute->getAttributeCode();
$magentoAttributeOptions = $this->attributeOptionManagement->getItems(
'catalog_product',
$attributeCode
);
$attributeOptions = ['Test2', 'Test3'];
$existingMagentoAttributeOptions = [];
$newOptions = [];
$counter = 0;
foreach($magentoAttributeOptions as $option) {
if (!$option->getValue()) {
continue;
}
if($option->getLabel() instanceof \Magento\Framework\Phrase) {
$label = $option->getText();
} else {
$label = $option->getLabel();
}
if($label == '') {
continue;
}
$existingMagentoAttributeOptions[] = $label;
$newOptions['value'][$option->getValue()] = [$label, $label];
$counter++;
}
foreach ($attributeOptions as $option) {
if($option == '') {
continue;
}
if(!in_array($option, $existingMagentoAttributeOptions)) {
$newOptions['value']['option_'.$counter] = [$option, $option];
}
$counter++;
}
if(count($newOptions)) {
$magentoAttribute->setOption($newOptions)->save();
}